Neat Trick for Loading Modules with unknown Name

Peter Hansen peter at
Tue Mar 23 01:58:03 CET 2004

Roger Binns wrote:
>>__import__ doesn't handle packages, right?  The above with exec()
>>appears, unfortunately, to be the simplest approach(*) to handling
>>*arbitrary* imports, even if they are nested as in xml.sax.whatever.
> Depends on your definition of *arbitrary* :-)  

True.  So I guess we could say the exec() approach is the simplest
way to do arbitrary *normal* imports, while __import__ can handle
arbitrary "simple" imports as-is, but neither alone is sufficient
for the entire possible range of (arbitrary) imports.  :-)


More information about the Python-list mailing list