import unknown module name

Thomas Heller theller at python.net
Fri Jan 10 08:47:41 CET 2003


Andrew Dalke <adalke at mindspring.com> writes:

> In general, __import__ returns the top-level module,
> and not the submodules.  You'll need to do
> 
>    codepage = getattar(__import__(pagename), "oldpage")
> 
> Also, use "except ImportError" instead of a blanket except.
> 
> If there's more than one level, this should work (untested)
> 
>     codepage = charset
>     terms = pagename.split(".")
>     for term in terms[1:]:
>         codepage = getattr(codepage, term)
> 
> I need this just enough to be annoying ....
> 

Doesn't

      codepage = sys.modules[pagename]

also work? At least no loop needed...

Thomas




More information about the Python-list mailing list