为什么Transcrypt编译在一个Python脚本中不起作用:操作系统('python m transcrypt b m n<somePythonFile>.py')?

2024-06-09 14:44:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python文件JS.py公司在为其编写Python函数体之后,我尝试动态地将其转换为JS。以下是目前为说明问题而提供的文件:

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

我有.py文件(translate2JS.py公司)在一个叫做translate2JS的文件夹里。我在做下面的工作视图.pyDjango项目:

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公司,它确实有效,而且JS.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的功能失效。有人知道问题是什么吗?如果有办法我可以解决它吗?你知道吗


Tags: 文件infrompyrightmaposmain
1条回答
网友
1楼 · 发布于 2024-06-09 14:44:41

我找到了问题的答案!我试着寻找更一般的操作系统我学到了一些东西。如前所述here,操作系统不是这样做的。较新且功能更强大的子流程模块提供了对命令执行方式的更大控制。所以:

添加行

import subprocess

并更改以下内容:

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

收件人:

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

相关问题 更多 >