3
votes

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.

1

1 Answers

8
votes

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