Problem with python 3.2 and circular imports
Frank Millman
frank at chagford.com
Sun Feb 27 08:33:24 EST 2011
"Ben Finney" <ben+python at benfinney.id.au> wrote in message
news:87aahh6401.fsf at benfinney.id.au...
> "Frank Millman" <frank at chagford.com> writes:
>
>> Assume the following structure -
>>
>> main.py
>> /pkg
>> __init__.py
>> mod1.py
>> mod2.py
>>
>> main.py
>> from pkg import mod1
>>
>> mod1.py
>> import mod2
>>
>> mod2.py
>> import mod1
>
> What are you expecting the result to be?
>
Simply that mod1 can refer to objects in mod2, and mod2 can refer to objects
in mod1.
> If it's about sharing objects between the modules, why not break the
> circular dependency: factor out the common code to a module that both
> the others import?
>
>> Any comments or suggestions will be appreciated.
>
> Special cases aren't special enough to break the rules. If you think you
> have a practical reason to do so, it would be best to make it explicit
> when asking for help about this.
>
I am trying to understand what 'the rule' is. Your advice above suggests
that you are one of those who recommend that circular imports are best
avoided altogether. In an ideal world I would agree. However, the fact is
that, no doubt due to a mental block I have, I do find myself in this
situation from time to time, and I have not seen anything in the
documentation or other literature that says it is absolutely wrong.
Therefore, while I do try to avoid circular imports where possible, I would
also like to know how to manage it in situations where I don't see a simple
alternative.
>From everything I have read about how the import mechanism works, I don't
understand *why* the above construct fails. That is actually what I am
asking for help with.
Thanks
Frank
More information about the Python-list
mailing list