[Python-Dev] Broken import?

Nick Coghlan ncoghlan at gmail.com
Wed Apr 1 12:45:26 CEST 2009


Greg Ewing wrote:
> Nick Coghlan wrote:
> 
>> Jim Fulton's example in that tracker issue shows that with a bit of
>> creativity you can provoke this behaviour *without* using a from-style
>> import. Torsten Bronger later brought up the same issue that Fredrik did
>> - it prevents some kinds of explicit relative import that look like they
>> should be fine.
> 
> I haven't been following this very closely, but if there's
> something that's making absolute and relative imports
> behave differently, I think it should be fixed. The only
> difference between an absolute and relative import of the
> same module should be the way you specify the module.

That's exactly the problem though. Because of the difference in the way
the target module is specified, the way it is looked up is different:

'import a.b.c' will look in sys.modules for "a.b.c", succeed and work,
even if "a.b.c" is in the process of being imported.

'from a.b import c' (or 'from . import c' in a subpackage of "a.b") will
only look in sys.modules for "a.b", and then look on that object for a
"c" attribute. The cached "a.b.c' module in sys.modules is ignored.

It doesn't appear to be an impossible problem to solve, but it probably
isn't going to be easy to fix in a backwards compatible way.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list