__import__() with packages

Hung Jung Lu hungjunglu at yahoo.com
Tue Apr 6 03:47:43 EDT 2004


Marco Herrn <herrn at gmx.net> wrote in message news:<c4pdmu$2lnh1i$2 at ID-165840.news.uni-berlin.de>...
> Thanks,
> but in my application I read the names of the modules from a file,
> so I do not know them when writing (in this case I wouldn't know the
> name 'eggs'). Since the solution from Paul works for me, I will use
> that.

There is an easier way: all imported modules are listed in the
sys.modules namespace dictionary. So,

import sys
__import__('spam.eggs')
my_module = sys.modules['spam.eggs']

regards,

Hung Jung



More information about the Python-list mailing list