Question about circular imports

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Feb 27 15:59:52 CET 2012


Frank Millman wrote:
> Hi all
>
> I seem to have a recurring battle with circular imports, and I am trying to 
> nail it once and for all.
>
> Let me say at the outset that I don't think I can get rid of circular 
> imports altogether. It is not uncommon for me to find that a method in 
> Module A needs to access something in Module B, and a method in Module B 
> needs to access something in Module A. I know that the standard advice is to 
> reorganise the code to avoid this, and I try to do this where possible, but 
> for now I would like to address the question of how to handle the situation 
> if this is otherwise unavoidable.
>   
Hi Frank,

If you consider it unavoidable you've already lost. There is no reliable 
solution to circular import except refactoring the code to place all 
common code elsewhere:

wavbase.py
wavread.py (import wavbase)
wavwrite.py (import wavbase)


Any code required by both waread and wavwrite would end up in wavbase.
Is there anything that prevent you from doing this ?

JM



More information about the Python-list mailing list