[Tutor] __import__()

Emile van Sebille emile at fenx.com
Fri Sep 24 03:02:18 CEST 2010

On 9/23/2010 8:26 AM Pete said...
> Hiya,
> still working on my plugin architecture. I figured out how to import modules of which I don't know the name yet at compile time,
> by using __import__() instead of import.
> So that works fine when I want to have the equivalent of
> import spam
> ... by using
> __import__('spam')
> Question:
> what is the equivalent of
> from spam import *

Something you probably really don't want to do as it will pollute your 
namespace. But, if you insist, you could play with:

ActivePython (ActiveState Software Inc.) based on
Python 2.6.1 (r261:67515, Dec  5 2008, 13:58:38)
[MSC v.1500 32 bit (Intel)] on win32
 >>> xx = __import__('httplib')
 >>> dir(xx)

 >>> for ii in dir(xx): globals()[ii] = xx.__dict__[ii]
 >>> dir()

Note, this really is for consenting adults only, and changing globals() 
is not really supported and may change in the future.  Note that from 
the docs, globals() is defined to "Return a dictionary representing the 
current global symbol table" and not as "Return the dictionary holding 
the current global symbol table" so the fact that it works on may 
version (and perhaps all others) doesn't mean it'll work everywhere.



More information about the Tutor mailing list