Dynamic module/package import ???

Gordon McMillan gmcm at hypernet.com
Thu Jan 13 12:59:57 EST 2000


jim wrote:

> I'm trying to import a module from a package dynamically. The layout is
> like so...
> 
> /DataHandler
>   |- DataRow.py
>   |- MyReader.py
>   |- __init__.py
> 
> In class DataRow.DataRow I try...
> 
>   def setReader( self, reader )
>     mod = __import__( 'DataHandler.' + reader + 'Reader' )
> 
> ... but this just loads the __init__.py, so it seems that the __import__
> doesn't understand packages or something.
> 
> Any help?

You need to use the fromlist parameter, as "print 
__import__.__doc__" will tell you.

mod = __import__( 'DataHandler.' + reader + 'Reader' , 
globals(), {}, [reader+'Reader'])

Actually, "nonempty" is the keyword. [1] works just as well.

- Gordon




More information about the Python-list mailing list