Possible to import a module whose name is contained in a variable?
Peter Hansen
peter at engcorp.com
Mon Mar 7 09:35:40 EST 2005
Gerrit Holl wrote:
> Ed Leafe wrote:
>
>>On Mar 7, 2005, at 5:23 AM, Michael Hoffman wrote:
>>
>>>Avoiding exec (which is a statement, not a function) is much more
>>>important. Since it executes arbitrary code, you can get unpredictable
>>>results from it.
>>
>> Is there any way to use __import__ to replace the following:
>>
>>exec("from %s import *" % modulename)
> No.
Between the following two recipes, it seems unlikely that a simple
"No" is really correct:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/223972
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307772
A variation on some of the code therein should certainly be
able to do the equivalent of "from xx import *". (The addition
of support for __all__ is at least required for full emulation.)
-Peter
More information about the Python-list
mailing list