我有两个python模块:
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模块:
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'
错误是什么意思?我如何解决它?
Answers:
我遇到了此错误,因为实际上未导入模块。代码如下:
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
。
在ubuntu 18.04(virtualenv,python.3.6.x)上,以下重新加载代码段为我解决了该问题:
main.py
import my_module # my_module.py
from importlib import reload # reload
reload(my_module)
print(my_module)
print(my_modeule.hello())
哪里:
|--main.py
|--my_module.py
有关更多文档,请检查:这里
该订单进口的就是为什么我有问题的原因:
a.py
:
############
# this is a problem
# move this to below
#############
from b import NewThing
class ProblemThing(object):
pass
class A(object):
###############
# add it here
# from b import NewThing
###############
nt = NewThing()
pass
b.py
:
from a import ProblemThing
class NewThing(ProblemThing):
pass
只是它的外观的另一个示例,类似于RichieHindie的答案,但带有类。
我已经多次遇到这个问题,但是我并没有尝试更深入地研究它。现在我了解了主要问题。
这次我的问题是从不同的模块(例如以下)中导入序列化器(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
希望这对其他人有帮助。
问候,