[Python-Dev] __import__ problems

Hrvoje Niksic hrvoje.niksic at avl.com
Fri Nov 28 11:36:44 CET 2008


Mart Somermaa wrote:
> I meant that you have to
> 
>>>> import sys
> 
> only to access sys.modules (i.e. importing sys may not be necessary otherwise).

I understand that, but I'm arguing that that's a non-problem.  Importing 
sys is a regular thing in Python, not an exception.  You need sys to get 
to sys.argv, sys.exit, sys.stdout, etc. -- it's not like sys is an 
infrequently used module.  Since sys is always present, importing it is 
not an efficiency problem, either.

>>>> mod = __import__(modname, submodule=True)
> 
> with
> 
>>>> import sys
>>>> __import__(modname)
>>>> mod = sys.modules[modname]

"import sys" is normally located near the beginning of the file (and 
needed by other things), so the actual code snippet would really contain 
only those two lines, which don't strike me as bad.  Ideally, __import__ 
would simply return the "tail" imported module in the first place, but I 
don't think introducing a boolean keyword argument really improves the 
design.


More information about the Python-Dev mailing list