Rectangle 27 1

python AttributeError: 'module' object has no attribute?


You are getting the example from the trunk (aka devel) documentation. if you aren't using trunk version, try to find an example for your correct version.

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


GCNSocket\socket\GCNSocket.py
socket

Rename it so it does not collide with standard library module name.

You're using socket as a package name. It cause import of your package socket instead of standard library module socket.

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


I created a math module , which is already standard module.

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.

I suspected this and deleted the .py module but forgot to delete the .pyc which was still causing the error.

I used abc.py to write a test to demonstrate the import behavior in python, that bites me a lot...

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


import a.b, a.c

# ...

something(a.b)
something(a.c)
something(a.d)  # My addition, which failed.

I experienced this error because the module was not actually imported. The code looked like this:

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.

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


@S.Lott: Thank you. I just know that it is a circular dependencies problem.

@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

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.

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


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

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

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

If you really must have mutual imports in Python, the way to do it is to import them within a function:

Interesting; I wonder why the interpreter does not give a proper error message in this case?

Now a.py can safely do import b without causing problems.

Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

Thank you! I know for the first time that importing in different places make such a difference.

You have mutual top-level imports, which is almost always a bad idea.

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


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

Hope that helps someone

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

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


@S.Lott: Thank you. I just know that it is a circular dependencies problem.

@Stephen Hsu: Breaking circular dependencies is easy. It's already been asked on SO several times. stackoverflow.com/search?q=%5Bpython%5D+circular+dependency

Thank you! It is what I guessed. But I cannot find some documents mention it. If I do need two modules import some attributes from each other, what should I do?

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.

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


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

Hope that helps someone

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

Note
Rectangle 27 1

python AttributeError: 'module' object has no attribute?


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

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

If you really must have mutual imports in Python, the way to do it is to import them within a function:

Interesting; I wonder why the interpreter does not give a proper error message in this case?

Now a.py can safely do import b without causing problems.

Please be aware that this adds over-head when the function is called, as you put the import logic at function call time, rather than program load time.

Thank you! I know for the first time that importing in different places make such a difference.

You have mutual top-level imports, which is almost always a bad idea.

Note