tags:

views:

73

answers:

1

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

http://stackoverflow.com/questions/2139202/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.

+5  A: 

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

digEmAll
Ah, the module to be imported still takes the name of the script the assembly was created from, not the assembly itself. I can confirm this to be true.
Jeff M
Thanks but this did not solve the problem as the phrase 'MyClass' does not appear anywhere in my project as anything but 'MyClass'. I think Jeff M's comment regarding versions is my next avenue of investigation. Which version of IronPython and .NET are you using? Would sure like to hear from the devs about this.
cory
Try to rename your .py differently but not MyClass.py. Anyway, I succesfully ran it targeting both .NET 2.0/4.0, with IronPython.dll 2.6.10920.0
digEmAll
In my tests, I created a script named `Program.py`, created an assembly named `CompiledTest.dll`. With the given code, and the appropriate assembly name change, I had the issue you (@cory) had. Changing the name of the module import to `Program` corrects this. Hopefully that answers your questions.
Jeff M
I am reopening this question, and at the end of the day I am filing a bug report if it is unsolved. My problem is not what you described. Sometimes when I compile with pyc.py, the dll produced works for ImportModule(). A simple recompilation this morning brought the bug back for me, so it seems to me to be random.
cory
Yes, compiling with pyc.py i have the same problem. I edited the question about it... ;)
digEmAll