circular import problem

  • Thread starter Learning Python
  • Start date
L

Learning Python

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,
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
 
T

Terry Reedy

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top