为什么Transcrypt编译无法在以下一行的Python脚本中运行:os.system('python -m transcrypt -b -m -n <somepythonfile> .py')?

时间:2018-10-24 04:19:57

标签: javascript python compilation translation transcrypt

我有一个Python文件translation2JS.py,在编写Python函数体后,我试图将其动态转换为JS。为了说明问题,以下是当前文件:

def tempFunc():
    for i in range(25):
         navigator.move("right")      

我在一个名为translate2JS的文件夹中具有.py文件(translate2JS.py)。我正在Django项目的views.py函数中执行以下操作:

os.system('ls') #check initial directory
os.chdir('main/static/main/js/translate2JS')
os.system('ls') #check right directory
os.system('python -m transcrypt -b -m -n translate2JS.py')# THIS is creating an empty file but if the command is entered
# in terminal it works as intended...# also, may need to change python to python3.6 when uploading
os.chdir('../../../../../')
os.system('ls') # check right directory

我检查了当前工作目录是否正确,并且上一行确实创建了.js文件,但是该文件仅包含以下内容:

// Transcrypt'ed from Python, 2018-10-24 00:01:12
import {AssertionError, AttributeError, BaseException, DeprecationWarning, Exception, IndexError, IterableError, KeyError, NotImplementedError, RuntimeWarning, StopIteration, UserWarning, ValueError, Warning, __JsIterator__, __PyIterator__, __Terminal__, __add__, __and__, __call__, __class__, __envir__, __eq__, __floordiv__, __ge__, __get__, __getcm__, __getitem__, __getslice__, __getsm__, __gt__, __i__, __iadd__, __iand__, __idiv__, __ijsmod__, __ilshift__, __imatmul__, __imod__, __imul__, __in__, __init__, __ior__, __ipow__, __irshift__, __isub__, __ixor__, __jsUsePyNext__, __jsmod__, __k__, __kwargtrans__, __le__, __lshift__, __lt__, __matmul__, __mergefields__, __mergekwargtrans__, __mod__, __mul__, __ne__, __neg__, __nest__, __or__, __pow__, __pragma__, __proxy__, __pyUseJsNext__, __rshift__, __setitem__, __setproperty__, __setslice__, __sort__, __specialattrib__, __sub__, __super__, __t__, __terminal__, __truediv__, __withblock__, __xor__, abs, all, any, assert, bool, bytearray, bytes, callable, chr, copy, deepcopy, delattr, dict, dir, divmod, enumerate, filter, float, getattr, hasattr, input, int, isinstance, issubclass, len, list, map, max, min, object, ord, pow, print, property, py_TypeError, py_iter, py_metatype, py_next, py_reversed, py_typeof, range, repr, round, set, setattr, sorted, str, sum, tuple, zip} from './org.transcrypt.__runtime__.js';
var __name__ = '__main__';

//# sourceMappingURL=translate2JS.map

现在,在终端中,如果我转到同一目录(main / static / main / js / translate2JS),然后键入python -m transcrypt -b -m -n translate2JS.py,它实际上可以正常运行,并且translate2JS .js文件最终看起来如下所示:

// Transcrypt'ed from Python, 2018-10-24 00:16:44
import {AssertionError, AttributeError, BaseException, DeprecationWarning, Exception, IndexError, IterableError, KeyError, NotImplementedError, RuntimeWarning, StopIteration, UserWarning, ValueError, Warning, __JsIterator__, __PyIterator__, __Terminal__, __add__, __and__, __call__, __class__, __envir__, __eq__, __floordiv__, __ge__, __get__, __getcm__, __getitem__, __getslice__, __getsm__, __gt__, __i__, __iadd__, __iand__, __idiv__, __ijsmod__, __ilshift__, __imatmul__, __imod__, __imul__, __in__, __init__, __ior__, __ipow__, __irshift__, __isub__, __ixor__, __jsUsePyNext__, __jsmod__, __k__, __kwargtrans__, __le__, __lshift__, __lt__, __matmul__, __mergefields__, __mergekwargtrans__, __mod__, __mul__, __ne__, __neg__, __nest__, __or__, __pow__, __pragma__, __proxy__, __pyUseJsNext__, __rshift__, __setitem__, __setproperty__, __setslice__, __sort__, __specialattrib__, __sub__, __super__, __t__, __terminal__, __truediv__, __withblock__, __xor__, abs, all, any, assert, bool, bytearray, bytes, callable, chr, copy, deepcopy, delattr, dict, dir, divmod, enumerate, filter, float, getattr, hasattr, input, int, isinstance, issubclass, len, list, map, max, min, object, ord, pow, print, property, py_TypeError, py_iter, py_metatype, py_next, py_reversed, py_typeof, range, repr, round, set, setattr, sorted, str, sum, tuple, zip} from './org.transcrypt.__runtime__.js';
var __name__ = '__main__';
export var tempFunc = function () {
    for (var i = 0; i < 25; i++) {
        navigator.move ('right');
    }
};

//# sourceMappingURL=translate2JS.map

关于在Python脚本中运行命令的某些事情使Transcrypt的功能丧失了。有谁知道这个问题是什么,是否有办法可以解决?

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案!我尝试搜索更通用的os.system问题,而不是专门搜索Transcrypt问题,并且我学到了一些东西。如here所讨论的,os.system并非做到这一点的方法。更新且功能更强大的 subprocess 模块可更好地控制命令的执行方式。因此:

添加行

import subprocess

并更改以下内容:

os.system('python -m transcrypt -b -m -n <fileToTranslate>.py')

至:

subprocess.call('python -m transcrypt -b -m -n <fileToTranslate>.py')