I have read several more questions on stackoverflow, but still don't understand.
From an example in another question, I created 2 simple modules that call each other to understand the circular import in python, but this has only generated more confusion for me.
I have the following 2 modules:
# module X.py
print('x1')
def X1():
return "x1"
from Y import Y2
print('x2')
def X2():
return "x2"
# module Y.py
print('y1')
def Y1():
return "y1"
from X import X1
print('y2')
def Y2():
return "y2"
When I run X.py an error happens:
x1
y1
x1
Traceback (most recent call last):
File "X.py", line 7, in <module>
from Y import Y2
File "***/Y.py", line 6, in <module>
from X import X1
File "***/X.py", line 7, in <module>
from Y import Y2
ImportError: cannot import name 'Y2' from 'Y' (***/Y.py)
When I run Y.py, no error happens, but I don't understand the order of the print.
y1
x1
y1
y2
x2
y2
I use python 3.8.3.