AttributeError: ‘module’ object has no attribute


I have two python modules:

import b

def hello():
  print "hello"

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

import a

def hi():
  print "hi"

When I run, I get:

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

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


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, it’s a quick operation.)


Leave a Reply

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