当模块引用出现问题时,可能存在两种被忽视的情况:
一、循环引用
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
When I run a.py
, I get:
AttributeError: 'module' object has no attribute 'hi'
Answer:
The problem is the circular dependency between the modules. a
imports b
and b
imports a
. But one of them needs to be loaded first - in this case python ends up initializing module a
before b
and b.hi()
doesn't exist yet when you try to access it in a
.
二、模块的重复引用
这个是在sikuli使用过程中遇到的,在其中我导入自己的模块screenShot.py一直出错,我在Python2.6以及Python2.7的环境下一直是正常的,但是在sikuli的集成环境下就不正常了,后来查了很多资料,发现可能是引用模块冲突问题导致,将screenShot.py改了名字就可以引用成功,希望帮助有此问题的同学~~