我有两个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 2.7和numpy 1.15.0版本工作的情况下,它与

pip install statsmodels=="0.10.0"

其他回答

导入的顺序是我遇到问题的原因:

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

这只是另一个例子,类似于richieindie的答案,但是有类。

我遇到这个错误是因为实际上没有导入模块。代码是这样的:

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。

解决了

Python正在你的a.py模块中寻找a对象。

将该文件重命名为其他文件或使用

from __future__ import absolute_import 

在a.py模块的顶部。

我曾多次遇到这个问题,但我并没有试图深入挖掘。现在我明白主要问题了。

这一次,我的问题是从不同的模块导入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

希望这能对其他人有所帮助。

问候,

问题在于模块之间的循环依赖关系。a导入b, b导入a。但其中一个需要先加载——在这种情况下,python最终会在b之前初始化模块a,而当你试图在a中访问它时,b.hi()还不存在。