Circular imports

jim-on-linux inq1ltd at verizon.net
Tue May 29 10:39:04 EDT 2007


On Tuesday 29 May 2007 00:08, Carsten Haese wrote:
> 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. 

I agree, waite until python complains.

You might try to remove all of the import 
statements then add then as they are requested by 
the program by a traceback error.


jim-on-linux


> 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