我有两个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'

这个误差是什么意思?我该怎么解决呢?


当前回答

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

这一次,我的问题是从不同的模块导入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.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的答案,但是有类。

你可以通过添加2个print来理解:

a.py:

print(__name__)
import b

b.py:

print(__name__)
import a

然后:

$ python3 a.py
__main__
b
a

a.py最终会被加载/执行2次。一个作为__main__,一个作为a。

我还曾在无意中将一个模块命名为标准Python模块时遇到过这种错误。例如,我有一个名为commands的模块,它也是一个Python库模块。这被证明很难追踪,因为它在我的本地开发环境中正确工作,但在谷歌应用程序引擎上运行时失败了。

对我来说,这个错误的原因是有一个文件夹与我试图导入的python模块同名。

|-- core  <-- empty directory on the same level as the module that throws the error
|-- core.py

python将该文件夹视为python包,并试图从空包“core”导入,而不是从core.py导入。

似乎出于某种原因,git在切换分支时留下了空文件夹

所以我移除了那个文件夹,一切都很顺利

您有相互的顶级导入,这几乎总是一个坏主意。

如果你真的必须在Python中有相互导入,方法是在函数中导入它们:

# In b.py:
def cause_a_to_do_something():
    import a
    a.do_something()

现在a.py可以安全地导入b而不会产生问题。

(乍一看,cause_a_to_do_something()似乎效率非常低,因为每次调用它时它都会导入,但实际上导入工作只在第一次执行。第二次和以后导入一个模块时,这是一个快速操作。)