[Python-Dev] relative import circular problem

Kristján Valur Jónsson kristjan at ccpgames.com
Fri Apr 5 13:23:50 CEST 2013

> -----Original Message-----
> From: PJ Eby [mailto:pje at telecommunity.com]
> Sent: 4. apríl 2013 20:29
> To: Guido van Rossum
> Cc: Kristján Valur Jónsson; Nick Coghlan; python-dev at python.org
> Subject: Re: [Python-Dev] relative import circular problem
> So, this is actually an implementation quirk that could be fixed in a
> (somewhat) straightforward manner: by making "from a import b" succeed if
> 'a.b' is in sys.modules, the same way "import a.b" does.  It would require a
> little bit of discussion to hash out the exact way to do it, but it could be done.

Yes, except that "from a import b" is not only used to import modules.  It is pretty much defined to mean "b = getattr(a, 'b')".  Consider this module:
# pull helper.helper from its implementation place
from .helper import helper

now, "from foo import helper" is expected to get the helper() function, not the helper module, but changing the semantics of the import statement would be "surprising".

More information about the Python-Dev mailing list