[IronPython] Importing embedded .py files

Michael Foord fuzzyman at voidspace.org.uk
Tue Jul 4 09:08:16 CEST 2006

JoeSox wrote:
> This is working except two of the embedded .py files import each
> other, so no matter which one I place first, it errors out.  
Try creating the empty modules and putting them in sys.modules first. I 
think this is basically what the import machinery does in cases of 
circular imports.

Michael Foord

> So I
> guess I can catch it. lol. It's going to end up being about 60+ lines
> of code to import 9 modules into the engine.
> Or, I could modify the .py files some more but I prefer not to.
> On 7/3/06, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
>> As a workaround, you could create your own module objects very easily
>> assuming you have the source code as a string.
>> import imp
>> import sys
>> newModule = imp.new_module(moduleName)
>> exec(sourceCode, newModule.__dict__)
>> sys.modules[moduleName] = newModule
>> You *should* then be able to import from this module in the normal way.
>> (Unless I have made a mistake in the above code.)
>> All the best,

More information about the Ironpython-users mailing list