Cyclic imports

Dan Bishop danb_83 at yahoo.com
Fri Jun 27 00:00:09 EDT 2008


On Jun 26, 10:40 pm, James <rent.lupin.r... at gmail.com> wrote:
> Hi all,
> I'm looking for some advice dealing with cyclic, cross-package
> imports.
>
> I've created the following demo file structure:
> ./a/__init__.py
> ./a/a.py
> ./b/__init__.py
> ./b/b.py
> ./main.py
>
> a.py imports a class from b.py and vice versa, and main.py imports
> them both.
>
> However, because a.py has not been completely read by the time it gets
> to the:
> from b import b
> line, the
> from a import a
> line in b.py fails with an import error (stack trace at the end).
>
> I hope that's clear: basically, because of the cyclic import, I see
> incomplete modules which can't be imported.
>
> Can I have this module hierarchy in some other way?

# a.py
import b
# refer to b.b

# b.py
import a
# refer to a.a



More information about the Python-list mailing list