Cross-module imports?

Ant antroy at gmail.com
Mon Jan 23 10:20:42 EST 2006


You are always likely to have problems of one sort or another if you
are using circular references like this. It would be a much better
situation redesigning your modules so that they didn't depend on each
other.

The interpreter is complaining, since it is trying to compile A, hits
the line 'import B', goes straight off into B to compile that, where it
encounters the line 'print A.var1', of course there is no 'A.var1' yet,
since the compiler hasn't yet got that far. So the error message is
perfectly sane (the codes author on the other hand... ;-) )

I would imagine that you *could* get it to work if you first tried to
run the example with line 3 of module B commented out (the thing should
compile fine then) and then run it again with line 3 uncommented again.
This way A will be already compliled when B comes to import it.

If you have to trick the compiler like this though, I'd take a good
look at *why* you want to couple the modules so tightly in the first
place!




More information about the Python-list mailing list