我有两个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'
这个误差是什么意思?我该怎么解决呢?
当前回答
我也遇到过同样的问题。 使用重载修复。
import the_module_name
from importlib import reload
reload(the_module_name)
其他回答
让我们通过例子来看看问题和解决方案,循环依赖关系是如何到达的。 我有一个文件窗口数据生成器。ipynb执行主文件导入如下两个文件。
escape.py MutationTypes.py
escape.py已经导入MutationTypes.py文件 现在,在窗口数据生成器中。我想执行MutationTypes.py的功能,如下所示:
import escape as ESC
import MutationTypes
MutationTypes.SINGLE_RES_SUB
错误提示为AttributeErrorTraceback(最近一次调用)/tmp/ipykernel_4340/4282764781.py在<module> ----> 1 MutationTypes中。属性错误:模块“MutationTypes”没有属性“SINGLE_RES_SUB”
如何解决这个问题? 因为您已经在转义模块中导入了MutationTypes文件,下面使用转义模块使用MutationTypes文件功能
ESC.MutationTypes.SINGLE_RES_SUB
我曾多次遇到这个问题,但我并没有试图深入挖掘。现在我明白主要问题了。
这一次,我的问题是从不同的模块导入Serializers (django和restframework),如下所示:
from rest_framework import serializers
from common import serializers as srlz
from prices import models as mdlpri
# the line below was the problem 'srlzprod'
from products import serializers as srlzprod
我遇到了一个这样的问题:
from product import serializers as srlzprod
ModuleNotFoundError: No module named 'product'
我想要完成的是:
class CampaignsProductsSerializers(srlz.DynamicFieldsModelSerializer):
bank_name = serializers.CharField(trim_whitespace=True,)
coupon_type = serializers.SerializerMethodField()
promotion_description = serializers.SerializerMethodField()
# the nested relation of the line below
product = srlzprod.ProductsSerializers(fields=['id','name',],read_only=True,)
因此,正如上面提到的如何解决它(顶级导入),我继续做以下更改:
# change
product = srlzprod.ProductsSerializers(fields=['id','name',],read_only=True,)
# by
product = serializers.SerializerMethodField()
# and create the following method and call from there the required serializer class
def get_product(self, obj):
from products import serializers as srlzprod
p_fields = ['id', 'name', ]
return srlzprod.ProductsSerializers(
obj.product, fields=p_fields, many=False,
).data
因此,django runserver的执行没有问题:
./project/settings/manage.py runserver 0:8002 --settings=settings_development_mlazo
Performing system checks...
System check identified no issues (0 silenced).
April 25, 2020 - 13:31:56
Django version 2.0.7, using settings 'settings_development_mlazo'
Starting development server at http://0:8002/
Quit the server with CONTROL-C.
代码行的最终状态如下:
from rest_framework import serializers
from common import serializers as srlz
from prices import models as mdlpri
class CampaignsProductsSerializers(srlz.DynamicFieldsModelSerializer):
bank_name = serializers.CharField(trim_whitespace=True,)
coupon_type = serializers.SerializerMethodField()
promotion_description = serializers.SerializerMethodField()
product = serializers.SerializerMethodField()
class Meta:
model = mdlpri.CampaignsProducts
fields = '__all__'
def get_product(self, obj):
from products import serializers as srlzprod
p_fields = ['id', 'name', ]
return srlzprod.ProductsSerializers(
obj.product, fields=p_fields, many=False,
).data
希望这能对其他人有所帮助。
问候,
我通过引用一个以错误方式导入的enum得到了这个错误,例如:
from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member
正确的导入:
from package.MyEnumClass import MyEnumClass
希望这能帮助到别人
您有相互的顶级导入,这几乎总是一个坏主意。
如果你真的必须在Python中有相互导入,方法是在函数中导入它们:
# In b.py:
def cause_a_to_do_something():
import a
a.do_something()
现在a.py可以安全地导入b而不会产生问题。
(乍一看,cause_a_to_do_something()似乎效率非常低,因为每次调用它时它都会导入,但实际上导入工作只在第一次执行。第二次和以后导入一个模块时,这是一个快速操作。)
我遇到这个错误是因为实际上没有导入模块。代码是这样的:
import a.b, a.c
# ...
something(a.b)
something(a.c)
something(a.d) # My addition, which failed.
最后一行导致一个AttributeError。原因是我没有注意到a的子模块(a.b和a.c)被显式导入,并假设import语句实际上导入了a。