I have two python modules:
我有两个python模块:
a.py
a.py
import b
def hello():
print "hello"
print "a.py"
print hello()
print b.hi()
b.py
b.py
import a
def hi():
print "hi"
When I run a.py
, I get:
当我运行。py,得到:
AttributeError: 'module' object has no attribute 'hi'
What does the error mean? How do I fix it?
误差是什么意思?我怎么修理它?
5 个解决方案
#1
127
You have mutual top-level imports, which is almost always a bad idea.
您有相互的*导入,这几乎总是一个坏主意。
If you really must have mutual imports in Python, the way to do it is to import them within a function:
如果您确实必须在Python中有相互导入,那么要做的方法是将它们导入到一个函数中:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
现在一个。py可以安全地导入b而不会产生问题。
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
(乍看之下,cause_a_to_do_something()可能会非常低效,因为每次您调用它时它都会导入,但实际上导入工作是第一次完成的。第二个和随后的时间,您导入一个模块,这是一个快速操作。
#2
65
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
我也看到了这个错误,在无意中给一个与标准Python模块同名的模块命名。我有一个模块叫命令,它也是一个Python库模块。当它在我的本地开发环境中正确地运行时,这是很难找到的,但是在谷歌应用程序引擎上运行时失败了。
#3
35
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
.
问题是模块之间的循环依赖关系。导入b和b导入a,但是其中一个需要先加载——在这种情况下,python会在b和b.hi()之前结束初始化模块a,而当您尝试访问它时,它还不存在。
#4
14
I got this error by referencing an enum which was imported in a wrong way, e.g.:
我引用了一个以错误的方式导入的enum,从而得到了这个错误。
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
正确的导入:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
希望能帮助一些人
#5
6
I experienced this error because the module was not actually imported. The code looked like this:
我经历了这个错误,因为这个模块实际上不是导入的。代码是这样的:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
最后一行产生了一个AttributeError。原因是我没有注意到a (a)的子模块。b和a.c)被显式导入,并假设import语句实际上导入了a。
#1
127
You have mutual top-level imports, which is almost always a bad idea.
您有相互的*导入,这几乎总是一个坏主意。
If you really must have mutual imports in Python, the way to do it is to import them within a function:
如果您确实必须在Python中有相互导入,那么要做的方法是将它们导入到一个函数中:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
Now a.py can safely do import b
without causing problems.
现在一个。py可以安全地导入b而不会产生问题。
(At first glance it might appear that cause_a_to_do_something()
would be hugely inefficient because it does an import
every time you call it, but in fact the import work only gets done the first time. The second and subsequent times you import a module, it's a quick operation.)
(乍看之下,cause_a_to_do_something()可能会非常低效,因为每次您调用它时它都会导入,但实际上导入工作是第一次完成的。第二个和随后的时间,您导入一个模块,这是一个快速操作。
#2
65
I have also seen this error when inadvertently naming a module with the same name as one of the standard Python modules. E.g. I had a module called commands
which is also a Python library module. This proved to be difficult to track down as it worked correctly on my local development environment but failed with the specified error when running on Google App Engine.
我也看到了这个错误,在无意中给一个与标准Python模块同名的模块命名。我有一个模块叫命令,它也是一个Python库模块。当它在我的本地开发环境中正确地运行时,这是很难找到的,但是在谷歌应用程序引擎上运行时失败了。
#3
35
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
.
问题是模块之间的循环依赖关系。导入b和b导入a,但是其中一个需要先加载——在这种情况下,python会在b和b.hi()之前结束初始化模块a,而当您尝试访问它时,它还不存在。
#4
14
I got this error by referencing an enum which was imported in a wrong way, e.g.:
我引用了一个以错误的方式导入的enum,从而得到了这个错误。
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
Correct import:
正确的导入:
from package.MyEnumClass import MyEnumClass
Hope that helps someone
希望能帮助一些人
#5
6
I experienced this error because the module was not actually imported. The code looked like this:
我经历了这个错误,因为这个模块实际上不是导入的。代码是这样的:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
The last line resulted in an AttributeError
. The cause was that I had failed to notice that the submodules of a
(a.b
and a.c
) were explicitly imported, and assumed that the import
statement actually imported a
.
最后一行产生了一个AttributeError。原因是我没有注意到a (a)的子模块。b和a.c)被显式导入,并假设import语句实际上导入了a。