Question about circular imports

Ian Kelly ian.g.kelly at gmail.com
Mon Feb 27 20:37:02 EST 2012


On Mon, Feb 27, 2012 at 6:01 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 2/27/2012 1:16 AM, Frank Millman wrote:
>>>
>>>
>>> To avoid the tedious reference, follow this with
>>> read = sound.formats.wavread # choose the identifier you prefer
>
>
> I tested something like this with stdlib, but there must be some important
> difference I did not notice. It make be in the contents of __init__.py.

I don't know what you tried, but I can easily replicate Frank's
results with an empty __init__.py.

> How about import b.d as d, etc?

> Try import b; c = b.c

Why would any of these make a difference?  The AttributeError
indicates that at the time d tries to grab the module in b's "c"
attribute, the attribute does not exist (because it has not finished
importing).  Alternate syntaxes for getting the "c" attribute from b
are not going to change that.

Python does some magic so that you can do "import b.c" inside d while
b.c is still importing without resulting in infinite recursion, but
the success of the import does not signify that b.c is actually
available yet.

Cheers,
Ian



More information about the Python-list mailing list