[Python-ideas] Avoid circular import
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  for a
minimal example demonstrating this problem.
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  for details.
More information about the Python-ideas