Cyclic imports

Torsten Bronger bronger at physik.rwth-aachen.de
Fri Jun 27 02:02:39 EDT 2008


Hallöchen!

James writes:

>> # a.py
>> import b
>> # refer to b.b
>>
>> # b.py
>> import a
>> # refer to a.a
>
> Thanks Dan, but that still doesn't work for me I'm afraid...
>
> I renamed the modules avoid name overloading -- a.py is now:
> import b
>
> class A():
>     print('b.b_mod:', b.b_mod)

Dan's hint is the way to go nevertheless.  However, it is still not
possible to actually use the module object on the top-level (i.e.,
while module a.py is read).  So, you must delay any access to module
b until everything is fully loaded -- for example, by wrapping the
access in a function which is called from the main program.

On the other hand, the above code was for debugging purposes I
assume.  So maybe there's no real problem anyway because all your
uses of module b are wrapped in functions/methods anyway.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
                   Jabber ID: torsten.bronger at jabber.rwth-aachen.de



More information about the Python-list mailing list