Question about circular imports

Frank Millman frank at chagford.com
Sun Feb 26 12:18:20 CET 2012


"Frank Millman" <frank at chagford.com> wrote in message 
news:jid2a9$n21$1 at dough.gmane.org...
> Hi all
>
> I seem to have a recurring battle with circular imports, and I am trying 
> to nail it once and for all.
>
[...]
>
> The second solution is -
>
> in formats/__init__.py
>    import sys
>    sys.path.insert(0, __path__[0])
>
> in wavread.py -
>    import wavwrite
>
> in wavwrite.py -
>    import wavread
>
> This works, but I don't know if it is a good idea to add all the 
> sub-package paths to sys.path. I realise that it is up to me to avoid any 
> name clashes. Are there any other downsides?
>

Answering my own question, I have just found a downside that is a 
showstopper.

If a module in a different sub-package needs to import one of these modules, 
it must use a full path. This results in a new entry in sys.modules, and 
therefore any attributes referenced by the intra-package module have 
different identities from those referenced from outside. If they are static, 
there is no problem, but if not, disaster strikes!

Frank






More information about the Python-list mailing list