Connecting Tech Pros Worldwide Forums | Help | Site Map

circular import problem

Learning Python
Guest
 
Posts: n/a
#1: Sep 9 '05
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,
[color=blue][color=green][color=darkred]
>>> import recur1[/color][/color][/color]
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


Terry Reedy
Guest
 
Posts: n/a
#2: Sep 9 '05

re: circular import problem



"Learning Python" <learningProgramming@gmail.com> wrote in message
news:1126288774.239916.310020@z14g2000cwz.googlegr oups.com...[color=blue]
> An example in the book I didn't understood well
> two modules files recursively import/from each other[/color]

There are past postings available in the archives (via Google) at least,
that lucided discuss circular imports. There are also some that recommend
"don't do that" and give alternatives. I recommend both.

Terry J. Reedy



Closed Thread


Similar Python bytes