Circular imports

Carsten Haese carsten at uniqsys.com
Tue May 29 00:08:36 EDT 2007


On Mon, 28 May 2007 23:46:00 -0400, Ron Provost wrote 
> [...] python is not happy about my circular imports [...]

A circular import is not a problem in itself. I'm guessing you're running into
a situation like this:

Module A imports module B and then defines an important class. Module B
imports A (which does nothing because A is already partially imported) and
then defines a class that depends on the class that is defined in module A.
That causes a NameError.

The root of the problem is that all statements are executed in the order in
which they appear, and B is imported before A had a chance to define the class
that B depends on.

Note that import statements don't have to be at the top of the file. Try
moving each import statement to the latest possible point in the code, i.e.
right before the first occurence of whatever names you're importing from the
respective modules. That way, each module gets to define as much as it
possibly can before it gets side-tracked by importing other modules.

If my guess doesn't help, you're going to have to post at least the
exception/traceback you're getting, and you're probably going to have to post
some code, too.

Good luck,

--
Carsten Haese
http://informixdb.sourceforge.net




More information about the Python-list mailing list