Transcrypt:使用来自另一个python脚本的代码会导致'TypeError:module is undefined'(Transcrypt: using code fro

编程入门 行业动态 更新时间:2024-10-24 08:26:16
Transcrypt:使用来自另一个python脚本的代码会导致'TypeError:module is undefined'(Transcrypt: using code from another python script causes 'TypeError: module is undefined')

我无法使用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.py

Which 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.py

So 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.

更多推荐

本文发布于:2023-07-15 05:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   代码   Transcrypt   python   TypeError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!