[Python-Dev] __import__ problems

Mart Somermaa mrts at mrts.pri.ee
Sun Nov 30 14:02:16 CET 2008

Brett Cannon wrote:

 > The old-hands on python-dev know this is where  I plug  my import
 > rewrite vaporware. It will be in 3.1, and as part of it there will be
 > a new API for handling direct imports. Jacob Kaplan-Moss and I have

Sounds good. I can finally rest in peace :) . May I suggest
that you disable the hack while you are at it as well so that
people will be aware of their misdoings?

 > talked about Django's need for this as PyCon so I am very aware of
 > needing this API and it will be addressed in the simplest way possible
 > (heck, the __import__ API might actually become a wrapper around the
 > simpler API in the end).

Even better (the bracketed part).

 > And since nothing can go into 3.0 anyway,
 > there is no need to rush into solving this right now.

Agreed, I just wanted to get the ball rolling.

Let me know if you want me to do some gruntwork (tests,
documentation) once the improved implementation is taking shape.


As for the other comments, I'll try to wrap things up:

 * I did get the impression from some posts that it was assumed
   to be somehow "my problem" -- although indeed seeing both
   'foo' and 'foo.' when printing sys.modules in a popular web
   framework I frequently use makes me wince in discomfort, the
   hack is present in 2000 lines in various projects as seen in
   the Google Code Search.

 * runpy.run_module() is not the solution as it runs the module
   each time it is called and particularly because access to the
   submodule object is generally needed (again, look around in
   the Google Code Search results).

 * changing the signature of __import__ is out of question both
   because it would break the existing __import__ replacements
   and would perpetuate the wrong assumption that it should be
   directly used (instead of the presently missing simpler


It looks that the

mod = sys.modules[modname]

idiom is the clear winner for the import submodule use case.
I've updated http://bugs.python.org/issue4457 with proposed
additions to current __import__ docs. Once the simpler interface
emerges, the docs should be updated again and __import__
use should be discouraged.

More information about the Python-Dev mailing list