【问题标题】:Why does Transcrypt compilation not work inside a Python script with the line: os.system('python -m transcrypt -b -m -n <somePythonFile>.py')?为什么 Transcrypt 编译在 Python 脚本中不起作用:os.system('python -m transcrypt -b -m -n <somePythonFile>.py')?
【发布时间】:2019-03-28 09:37:09
【问题描述】:

我有一个 Python 文件 translate2JS.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 的功能失效。有谁知道问题出在哪里,有什么办法可以解决吗?

【问题讨论】:

    标签: javascript python compilation translation transcrypt


    【解决方案1】:

    我找到了问题的答案!我尝试搜索更一般的 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')
    

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多