gpt4 book ai didi

javascript - 为什么 Transcrypt 编译在带有 : os. system ('python -m transcrypt -b -m -n .py' ) 行的 Python 脚本中不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:44 24 4
gpt4 key购买 nike

我有一个Python文件translate2JS.py,我试图在向其写入Python函数体后动态转换为JS。这是当前用于说明问题的文件:

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

我的 .py 文件 (translate2JS.py) 位于名为 translate2JS 的文件夹中。我正在 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 transcrypt2JS.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 的功能失效。有谁知道问题是什么以及是否有办法解决它?

最佳答案

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

关于javascript - 为什么 Transcrypt 编译在带有 : os. system ('python -m transcrypt -b -m -n <somePythonFile>.py' ) 行的 Python 脚本中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52961063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com