IronPython,导入模块
我按照此处最佳答案中的示例进行操作,使用 Pyc.py 进行编译。
我收到异常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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,
我得到了它。
模块名称是原始 .py 模块的名称(区分大小写),而不是编译后的 dll。
我的意思是,如果你的原始模块名称是
myClass.py
,那么你在MyClass.dll
中编译它,你必须ImportModule("myClass")
> 不是ImportModule("MyClass")
编辑:
前面的代码引用了以下编译方法:
相反,使用
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 inMyClass.dll
, you mustImportModule("myClass")
notImportModule("MyClass")
EDIT:
the previous code refers to the following compile method:
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...