importing modules with the same name but from different directories

Aahz aahz at pythoncraft.com
Wed Apr 30 09:45:56 EDT 2003


In article <T8Qra.32135$WQ4.29932 at news2.central.cox.net>,
Steve Holden <sholden at holdenweb.com> wrote:
>
>What you definitely *can't* do is this:
>
>    m = "modulename_or_path"
>    import m as something
>
>To emulate this non-available solution and actually execute an import you
>would need to use this:
>
>    m = "modulename_or_path"
>    exec "import %s as something" % m
>
>I'd advise against any solution that involves exec'ing or eval'ing unless
>there's really no alternative, which is not the case here. What you would
>need for this is the __import__() function, built in to the language and
>just waiting for you to take advantage of it.

Use the ``imp`` module.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"In many ways, it's a dull language, borrowing solid old concepts from
many other languages & styles:  boring syntax, unsurprising semantics,
few automatic coercions, etc etc.  But that's one of the things I like
about it."  --Tim Peters on Python, 16 Sep 93




More information about the Python-list mailing list