[Python-Dev] Broken import?

Nick Coghlan ncoghlan at gmail.com
Wed Apr 1 13:50:07 CEST 2009

Greg Ewing wrote:
> Nick Coghlan wrote:
>> '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.
> Hasn't 'from a.b import c' always been that way, though?
> Is the problem just that relative imports make it easier
> to run into this behaviour, or has something about the
> way imports work changed?

The former - while a few things have obviously changed in this area due
to PEP 328 and PEP 366, I don't believe any of that affected this aspect
of the semantics (the issue I linked dates from 2004!).

Instead, I'm pretty sure implicit relative imports use the 'import
a.b.c' rules and hence work in situations where explicit relative
imports now fail.


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

More information about the Python-Dev mailing list