[Python-ideas] Avoid circular import

Sven Marnach sven at marnach.net
Tue Apr 10 15:56:36 CEST 2012

Oleg Broytman schrieb am Tue, 10. Apr 2012, um 16:03:15 +0400:
>    I didn't and do not recommend merging code into one huge module. Call
> me dogmatic but I recommend to refactor and move common parts to avoid
> circular import.

I actually did ran into cases where this was not possible.  (I just
wrote a lengthy description of such a case, but I figured it wasn't
too helpful, so it's not included here.)

A point to consider is that there are cases of circular imports that
used to work fine with implicit relative imports.  When using explicit
relative imports though, they would stop working -- see [1] for a
minimal example demonstrating this problem.

[1]: http://stackoverflow.com/questions/6351805/cyclic-module-dependencies-and-relative-imports-in-python

This issue can be easily overcome with function-level imports, but
some people don't like function-level imports either.

The same issue turned up when porting the Python Imaging Library to
Python 3.  PIL uses implicit relative, circular imports which have to
be turned into function-level imports to work properly on Python 3,
see [2] for details.

[2]: https://github.com/sloonz/pil-py3k/pull/2


More information about the Python-ideas mailing list