print(x) from m2 import x y = 20
print(y) from m1 import y x = 1
如果运行m1.py,则会产生`ImportError: cannot import name 'y`
如果运行m2.py,则会产生`ImportError: cannot import name 'x`
所以根本原因是:在导入的时候,m1需要访问m2的变量y,但是y没有初始化完成
所以解决方法有两个:
1 print(x) 2 3 def func1(): 4 from m2 import x 5 print(x) 6 7 y =20
1 print(y) 2 3 def func1(): 4 from m1 import y 5 print(y) 6 7 x = 1
1 print(x) 2 3 y = 20 4 from m2 import x
1 print(y) 2 3 x = 1 4 from m1 import y