251

I have two python modules:

a.py

import b

def hello():
  print "hello"

print "a.py"
print hello()
print b.hi()

b.py

import a

def hi():
  print "hi"

When I run a.py, I get:

AttributeError: 'module' object has no attribute 'hi'

What does the error mean? How do I fix it?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Stephen Hsu
  • 5,127
  • 7
  • 31
  • 39
  • Note that your questions is very similar to this answer. Apparently the code in this answer works just find, but yours does not? http://stackoverflow.com/a/7336880/565879 – Buttons840 May 02 '14 at 18:15

20 Answers20

241

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:

# 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.

(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.)

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
117

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.

lucrussell
  • 5,032
  • 2
  • 33
  • 39
53

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.

sth
  • 222,467
  • 53
  • 283
  • 367
28

I got this error by referencing an enum which was imported in a wrong way, e.g.:

from package import MyEnumClass
# ...
# in some method:
return MyEnumClass.Member

Correct import:

from package.MyEnumClass import MyEnumClass

Hope that helps someone

Stoyan
  • 297
  • 3
  • 2
10

I faced the same issue. fixed by using reload.

import the_module_name
from importlib import reload
reload(the_module_name)
JustWe
  • 4,250
  • 3
  • 39
  • 90
8

on ubuntu 18.04 ( virtualenv, python.3.6.x), the following reload snippet solved the problem for me:

main.py

import my_module  # my_module.py
from importlib import reload # reload 
reload(my_module)

print(my_module)
print(my_modeule.hello())

where:

|--main.py    
|--my_module.py

for more documentation check : here

Behzad Sezari
  • 728
  • 8
  • 11
7

I ran into this problem when I checked out an older version of a repository from git. Git replaced my .py files, but left the untracked .pyc files. Since the .py files and .pyc files were out of sync, the import command in a .py file could not find the corresponding module in the .pyc files.

The solution was simply to delete the .pyc files, and let them be automatically regenerated.

craq
  • 1,441
  • 2
  • 20
  • 39
  • You can use this command to delete all `.pyc` files: `find . -name "*.pyc" -exec rm -f {} \;` – Ollie Jan 24 '19 at 02:26
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.

Dag Høidahl
  • 7,873
  • 8
  • 53
  • 66
5

All the above answers are great, but I'd like to chime in here. If you did not spot any issue mentioned above, try clear up your working environment. It worked for me.

Jian
  • 59
  • 1
  • 1
  • To supplement this answer: when Jian said "clear up your working environment", if you are in Jupyter notebook, you simply "shutdown" your working `.ipynb` (not the env or machine) and open your `.ipynb` again, it works for me. – Ellery Leung Jun 17 '22 at 09:45
  • or restart your IDA kernel as not everyone using Jupyter – gregV Apr 19 '23 at 18:27
4

Circular imports cause problems, but Python has ways to mitigate it built-in.

The problem is when you run python a.py, it runs a.py but not mark it imported as a module. So in turn a.py -> imports module b -> imports module a -> imports module b. The last import a no-op since b is currently being imported and Python guards against that. And b is an empty module for now. So when it executes b.hi(), it can't find anything.

Note that the b.hi() that got executed is during a.py -> module b -> module a, not in a.py directly.

In your specific example, you can just run python -c 'import a' at top-level, so the first execution of a.py is registered as importing a module.

Hot.PxL
  • 1,902
  • 1
  • 17
  • 30
  • 1
    This IS the correct answer IMHO. very well explained. – gst Oct 16 '21 at 07:34
  • I would add that when he runs `python a.py` it effectively (start to) runs it (what else), but not mark it as imported .. BECAUSE it IS the `__main__` module being executed. – gst Oct 16 '21 at 07:37
3

For me, the reason for this error was that there was a folder with the same name as the python module I was trying to import.

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

And python treated that folder as a python package and tried to import from that empty package "core", not from core.py.

Seems like for some reason git left that empty folder during the branches switch

So I just removed that folder and everything worked like a charm

DimaDK
  • 49
  • 2
  • 3
1

are you saved 'b.py' ? you must save 'b.py' first.

KBT K.B.T
  • 21
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '22 at 02:56
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31262212) – Ari Cooper-Davis Mar 14 '22 at 11:25
  • I recommend against rhetoric questions in answers. They risk being misunderstood as not an answer at all. You are trying to answer the question at the top of this page, aren't you? Otherwise please delete this post. – Yunnosch May 08 '22 at 20:21
  • Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ). For example like "If your problem is ... then the solution is to .... because .... ." – Yunnosch May 08 '22 at 20:21
0

You can understand what's going by adding 2 prints :

a.py :

print(__name__)
import b

b.py :

print(__name__)
import a

Then:

$ python3 a.py
__main__
b
a

so a.py ends up being loaded/executed 2 times. one as __main__ and one as a.

gst
  • 431
  • 1
  • 4
  • 16
0

In some cases it could be simply that the file is saved in the right directory but the python written on the file is not yet saved when writing it on the virtual environment. So when python imports a.py to b.py there is no code imported. It's blank in other terms. Seems like a easy to identify error that I see often. It's worth checking. -Cody

  • Welcome to StackOverflow. I could see the situation you describe being a problem and causing the error message shown in the question, but it's not really relevant to this particular question, where the problem was with the circular imports. – joanis Jan 05 '22 at 15:21
0

Let's see the problem and solution by example how the circular dependency arrives. I have a file window-data-generator.ipynb Main executing file Where the following two files are imported.

  1. escape.py
  2. MutationTypes.py

escape.py already have imported MutationTypes.py file Now, in window-data-generator.ipynb file I would like to execute the functionality of MutationTypes.py as below :

import escape as ESC
import MutationTypes
MutationTypes.SINGLE_RES_SUB 

The error prompts as AttributeErrorTraceback (most recent call last) /tmp/ipykernel_4340/4282764781.py in <module> ----> 1 MutationTypes.SINGLE_RES_SUB AttributeError: module 'MutationTypes' has no attribute 'SINGLE_RES_SUB'

How to solve this? As you already have MutationTypes file imported inside escape module, use MutationTypes file functionalities using escape module as below

ESC.MutationTypes.SINGLE_RES_SUB
Prem Singh Bist
  • 1,273
  • 5
  • 22
  • 37
-1

Not sure how but the below change sorted my issue:

i was having the name of file and import name same for eg i had file name as emoji.py and i was trying to import emoji. But changing the name of file solved the issue .

Hope so it helps

MD5
  • 1,356
  • 15
  • 14
-1

The order of the importing was the reason why I was having issues:

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

Just another example of how it might look, similar to RichieHindie's answer, but with classes.

jmunsch
  • 22,771
  • 11
  • 93
  • 114
-1

I have crossed with this issue many times, but I didnt try to dig deeper about it. Now I understand the main issue.

This time my problem was importing Serializers ( django and restframework ) from different modules such as the following :

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

I was getting a problem like this :

from product import serializers as srlzprod
ModuleNotFoundError: No module named 'product'

What I wanted to accomplished was the following :

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,)

So, as mentioned by the lines above how to solve it ( top-level import ), I proceed to do the following changes :

# 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

Therefore, django runserver was executed without problems :

./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.

Final state of the code lines was the following :

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

Hope this could be helpful for everybody else.

Greetings,

Manuel Lazo
  • 745
  • 7
  • 7
-1

SoLvEd

Python is looking for the a object within your a.py module.

Either RENAME that file to something else or use

from __future__ import absolute_import 

at the top of your a.py module.

hassanzadeh.sd
  • 3,091
  • 1
  • 17
  • 26
-2

In my case working with python 2.7 with numpy version 1.15.0, it worked with

pip install statsmodels=="0.10.0"
Mondaa
  • 127
  • 3
  • 4