1

我有一个 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 的功能失效。有谁知道问题是什么,如果有办法解决它?

4

1 回答 1

0

我找到了我的问题的答案!我尝试搜索更一般的 os.system 问题,而不是专门搜索 Transcrypt 问题,我学到了一些东西。正如这里所讨论的, 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')
于 2018-10-24T15:36:23.710 回答