3

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.

Community
  • 1
  • 1
cory
  • 341
  • 3
  • 15

1 Answers1

8

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
  • 56,430
  • 9
  • 115
  • 140
  • 1
    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 Mercado Sep 13 '10 at 22:01
  • 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 Sep 13 '10 at 22:02
  • 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 Sep 13 '10 at 22:08
  • 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 Mercado Sep 13 '10 at 22:08
  • 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 Sep 14 '10 at 18:07
  • Yes, compiling with pyc.py i have the same problem. I edited the question about it... ;) – digEmAll Sep 14 '10 at 21:41