python – AttributeError: module object has no attribute

python – AttributeError: module object has no attribute

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
def cause_a_to_do_something():
    import a

Now 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, its a quick operation.)

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.

python – AttributeError: module object has no attribute

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() doesnt exist yet when you try to access it in a.

Leave a Reply

Your email address will not be published. Required fields are marked *