AttributeError: 'module' object has no attribute

1.6K    Asked by Dhruvtiwari in Python , Asked on Apr 20, 2021

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?

Answered by Dhruv tiwari

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

def cause_a_to_do_something():
import a
a.do_something()

Note : Getting AttributeError: 'module' object has no attribute because there is no attribute with the name you called, for that Object.



Your Answer

Interviews

Parent Categories