how to import a module dynamically

Robert Brewer fumanchu at amor.org
Fri Jan 2 16:13:29 EST 2004


Randall Smith wrote:
> How do I import a module when given the module name as input?
> 
> Between __import__, imp, and rexec, I'm very lost.  What I want to do 
> seems so simple, but I don't know the best way to do it.

I was very lost, too, once upon a time. I wrote my eventual solution for
this into a Python Cookbook recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/223972

...which is geared toward importing when you know the full package name,
for example "myapp.tools.utilities". Feel free to hack it up for your
own study and/or use.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list