【发布时间】:2014-05-04 06:10:24
【问题描述】:
我正在尝试理解循环导入问题,这里我有三个 python 文件,
py1.py
import py3
py3.test3()
def test1():
print 'test 1'
py2.py
import py1
py1.test1()
def test2():
print 'test 2'
py3.py
import py2
py2.test2()
def test3():
print 'test 2'
当我运行python py3.py 并得到这样的错误时,
Traceback (most recent call last):
File "py3.py", line 1, in <module>
import py2
File "/home/me/Desktop/hackerearth/cylic/py2.py", line 1, in <module>
import py1
File "/home/me/Desktop/hackerearth/cylic/py1.py", line 1, in <module>
import py3
File "/home/me/Desktop/hackerearth/cylic/py3.py", line 3, in <module>
py2.test2()
AttributeError: 'module' object has no attribute 'test2'
但是当我从 py1.py 文件中删除 import py3 时,我得到的输出没有任何错误。任何人解释我为什么我得到这个错误。
【问题讨论】:
标签: python