我有两个python模块:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
当我运行a.py时,我得到:
AttributeError: 'module' object has no attribute 'hi'
这个误差是什么意思?我该怎么解决呢?
我有两个python模块:
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
import a
def hi():
print "hi"
当我运行a.py时,我得到:
AttributeError: 'module' object has no attribute 'hi'
这个误差是什么意思?我该怎么解决呢?
当前回答
在某些情况下,可能只是文件保存在正确的目录中,但在虚拟环境中写入文件时,文件中写入的python还没有保存。因此,当python将a.py导入到b.py时,不会导入任何代码。在其他方面是空白的。这似乎是我经常看到的一个容易识别的错误。值得一查。 科迪
其他回答
解决了
Python正在你的a.py模块中寻找a对象。
将该文件重命名为其他文件或使用
from __future__ import absolute_import
在a.py模块的顶部。
我通过引用一个以错误方式导入的enum得到了这个错误,例如:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
正确的导入:
from package.MyEnumClass import MyEnumClass
希望这能帮助到别人
对我来说,这个错误的原因是有一个文件夹与我试图导入的python模块同名。
|-- core <-- empty directory on the same level as the module that throws the error
|-- core.py
python将该文件夹视为python包,并试图从空包“core”导入,而不是从core.py导入。
似乎出于某种原因,git在切换分支时留下了空文件夹
所以我移除了那个文件夹,一切都很顺利
在我使用python 2.7和numpy 1.15.0版本工作的情况下,它与
pip install statsmodels=="0.10.0"
在某些情况下,可能只是文件保存在正确的目录中,但在虚拟环境中写入文件时,文件中写入的python还没有保存。因此,当python将a.py导入到b.py时,不会导入任何代码。在其他方面是空白的。这似乎是我经常看到的一个容易识别的错误。值得一查。 科迪