我无法使用transcrypt(版本3.6.95)在多个文件之间拆分代码。 作为一个基本示例,我在同一目录中有以下文件:
index.htm的
<html> <head> <meta charset="utf-8"> <title>Transcrypt test</title> </head> <body> <div id="box"></div> <button onclick="myscript.set_box_content()">Set box content</button> </body> <script src="__javascript__/myscript.js"></script> </html>mymodule.py
def helloworld(): return "Hello world!"myscript.py
from mymodule import helloworld def set_box_content(): document.getElementById("box").innerHTML = helloworld()然后我跑了
python -m transcrypt -n mymodule.py python -m transcrypt -n myscript.py哪个运行没有错误,并在目录__javascript__中生成mymodule.js,mymodule.mod.js,myscript.js和myscript.mod.js。
当我在Firefox 58中打开index.htm并打开控制台时,它会显示“TypeError:module is undefined”。 我尝试将<script src="__javascript__/mymodule.js"></script>到HTML中,但这没有帮助。 我仔细阅读了transcrypt文档的这一部分 ,但是当我键入python -m transcrypt -h时, -u开关没有出现在可用命令列表中。
I am having trouble splitting code across multiple files using transcrypt (version 3.6.95). As a basic example, I have the following files in the same directory:
index.htm
<html> <head> <meta charset="utf-8"> <title>Transcrypt test</title> </head> <body> <div id="box"></div> <button onclick="myscript.set_box_content()">Set box content</button> </body> <script src="__javascript__/myscript.js"></script> </html>mymodule.py
def helloworld(): return "Hello world!"myscript.py
from mymodule import helloworld def set_box_content(): document.getElementById("box").innerHTML = helloworld()I then run
python -m transcrypt -n mymodule.py python -m transcrypt -n myscript.pyWhich runs without error and generates mymodule.js, mymodule.mod.js, myscript.js and myscript.mod.js in the directory __javascript__.
When I open index.htm in Firefox 58 and open the console it says 'TypeError: module is undefined'. I have tried adding <script src="__javascript__/mymodule.js"></script> to the HTML but this does not help. I read through this part of the transcrypt documentation, but the -u switch does not appear in the list of available commands when I type python -m transcrypt -h.
最满意答案
单元(编译单元,组件)是一个相对较新的功能,而不是从一开始就在Transcrypt中的模块。 您需要Transcrypt 3.6.101才能使用单位。 请注意,由于CPython是一个解释器而不是编译器,因此编译单元的概念不起作用。
单元与模块组合的使用显示在:
https://transcrypt.org/docs/html/special_facilities.html#transcrypt-s-unit-mechanism-and-creating-native-javascript-component-frameworks
这个例子应该让你入门,如果没有,请在评论或编辑中告诉我。
[编辑]所有单位(与模块相对)应单独编译,因此在示例中:
transcrypt -u .run animals.py transcrypt -u .com cats.py transcrypt -u .com dogs.py所以模块包含带有.run选项的运行时和带有.com选项的其他组件。 如果需要,可以添加-n开关。
应将多个单元中使用的模块添加到运行时单元,即使用-u .run开关编译的单元。
Units (compilation units, components) are a relatively new feature, as opposed to modules, that have been in Transcrypt from the start. You need Transcrypt 3.6.101 to use units. Note that since CPython is an interpreter rather than a compiler, the concept of a compilation unit plays no role there.
The use of units in combination with modules is shown in:
https://transcrypt.org/docs/html/special_facilities.html#transcrypt-s-unit-mechanism-and-creating-native-javascript-component-frameworks
This example should get you started, if not please let me know in a comment or edit.
[EDIT] All units (as opposed to modules) should be compiled separately, so in the example:
transcrypt -u .run animals.py transcrypt -u .com cats.py transcrypt -u .com dogs.pySo the module containing the runtime with the .run option and the other components with the .com option. The -n switch can be added if desired.
Modules that are used in multiple units should be added to the runtime unit, that is the one compiled with the -u .run switch.
更多推荐
发布评论