Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IronPython, importing Modules

Tags:

c#

ironpython

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.

like image 398
cory Avatar asked May 19 '26 06:05

cory


1 Answers

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

like image 196
digEmAll Avatar answered May 21 '26 19:05

digEmAll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!