[Python-Dev] relative import circular problem

Nick Coghlan ncoghlan at gmail.com
Tue Apr 2 00:52:56 CEST 2013


On Tue, Apr 2, 2013 at 6:20 AM, Kristján Valur Jónsson <
kristjan at ccpgames.com> wrote:

>  I just ran into the issue described in
> http://stackoverflow.com/questions/6351805/cyclic-module-dependencies-and-relative-imports-in-python
> .
>
> This is unfortunate, because we have been trying to move towards relative
> imports in order to aid flexibility in package and library design.
>
> The relative import syntax
>
>   (from foo import bar) is a getattr type of lookup (i.e. import foo, then
> get attr from it).
>
> This is in contrast with absolute import
>
>   import foo.bar  (get the module foo.bar from sys.modules or import it)
>
>   bar = foo.bar
>
>
>
> the latter works with partially initialized modules, but not the former,
> rendering two sibling modules unable to import each other using the
> relative syntax.
>

This is really a quality-of-implementation issue in the import system
rather than a core language design problem. It's just that those of us with
the knowledge and ability to fix it aren't inclined to do so because
circular imports usually (although not quite always) indicate a need to
factor some common code out into a third support module imported by both of
the original modules. At that point, the code is cleaner and more
decoupled, and the uneven circular import support ceases to be a problem
for that application.

If you're interested in digging further, see
http://bugs.python.org/issue992389 (this should also be a *lot* easier to
fix now we're using importlib than it ever would have been while we were
still relying on import.c)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130402/66d6eb70/attachment.html>


More information about the Python-Dev mailing list