Importing dynamically loaded modules (in packages)?

holger krekel pyth at devel.trillke.net
Tue Apr 30 20:30:46 EDT 2002


On Wed, May 01, 2002 at 02:06:47AM +0200, Irmen de Jong wrote:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82234
> more or less confirms the code that I've created myself (in Pyro).
> But this code only works if the module does not come from a package.
> i.e. loading the .py file of "TestModule" and then "import TestModule"
> works fine, but loading "Package.TestModule.py" and then
> "import Package.TestModule" doesn't work.

> I've been toying around with creating package/modules structures
> with new.module, mimicing the package structure on disk, but
> even if I have this:
> 
> sys.modules['Package']= module 'Package'
>     module 'Package' . TestModule = the loaded module
> 
> "import Package.TestModule" still doesn't work: ""no module named TestModule""
> How can I correct this?

    sys.modules['Package.TestModule'] = TestModule 

after that you should be able to do

    import Package.TestModule

regards,

    holger





More information about the Python-list mailing list