IronPython,导入模块

发布于 2024-09-19 06:22:18 字数 456 浏览 0 评论 0原文

我按照此处最佳答案中的示例进行操作,使用 Pyc.py 进行编译。

构建 Python 脚本并从 C# 调用方法

我收到异常at pyScope = pyEngine.ImportModule("MyClass");

no module named MyClass

我相信这是一个错误,因为有时使用 Pyc.py 重新编译会生成一个 ImportModule 可以识别的 dll,但有时却不能。

结论:正如下面 digEmAll 所指出的,使用 Pyc.py 编译模块以这种方式使用会产生随机结果。请手动调用 clr.CompileModules。

I follow the example from the best answer here to a T, compiling with Pyc.py.

Build Python scripts and call methods from C#

I get an exception at pyScope = pyEngine.ImportModule("MyClass");

no module named MyClass

I believe this to be a bug as sometimes recompilation with Pyc.py will produce a dll ImportModule recognizes, but other times it doesn't.

CONCLUSION: As noted below by digEmAll, compiling modules with Pyc.py to be used in this fashion produces random results. Call clr.CompileModules manually instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-09-26 06:22:18

好的,
我得到了它。

模块名称是原始 .py 模块的名称(区分大小写),而不是编译后的 dll。

我的意思是,如果你的原始模块名称是 myClass.py,那么你在 MyClass.dll 中编译它,你必须 ImportModule("myClass") > 不是 ImportModule("MyClass")


编辑:

前面的代码引用了以下编译方法:

import clr
clr.CompileModules("CompiledScript.dll", "script.py")

相反,使用 pyc.py,生成的 dll 包含一个名为 __main__ 的模块,而不是 .py 文件名。

这很奇怪...

IIRC,在 python 中,如果模块独立运行(即不被另一个调用),则它会调用自己 __main__ ,但我仍然不明白其中的联系...

OK,
I got it.

The module name is the (case sensitive) name of the original .py module, not the compiled dll.

I mean, if your original module name was myClass.py, then you compiled it in MyClass.dll, you must ImportModule("myClass") not ImportModule("MyClass")


EDIT:

the previous code refers to the following compile method:

import clr
clr.CompileModules("CompiledScript.dll", "script.py")

On the contrary, using pyc.py, the generated dll contains a module called __main__ instead of the .py file name.

That's very strange...

IIRC, in python a module call itself __main__ if it's running standalone (i.e. not called by another), but I still don't grasp the connection...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文