circular import problem

Learning Python learningProgramming at gmail.com
Fri Sep 9 13:59:34 EDT 2005


An example in the book I didn't understood well
two modules files recursively import/from each other

in recur1.py,we have:

x=1
import recur2
y=1


in recur2.py, we have

from recur1 import x
from recur1 import y


If we run interactively at python command line,

>>> import recur1
it has errors like this:
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "recur1.py", line 2, in ?
    import recur2
  File "recur2.py", line 2, in ?
    from recur1 import y
ImportError: cannot import name y

I understood this because recur1 is not finished when recur2 is trying
to import y.

However, if you run interactive for recur2.py interactively, it is
fine.
when you run as script from command line, recur1.py is fine. but when
you
python recur2.py at commmand line, it has errors like this:
Traceback (most recent call last):
  File "recur2.py", line 1, in ?
    from recur1 import x
  File "recur1.py", line 2, in ?
    import recur2
  File "recur2.py", line 2, in ?
    from recur1 import y
ImportError: cannot import name y

What's really the problem here?

Thanks




More information about the Python-list mailing list