Python throws the error, ‘module’ object is not callable, when there is conflict with file name and function name and we try to use filename for calling.
This is a common error since its a practice to use file name same as class name.
Suppose you have a file
SuperHero.py and you defined a function
SuperHero() in it –
def SuperHero(realName = ''): if realName == 'Steve Rogers': return "Captain America" elif realName == 'Tony Stark': return "Ironman" elif realName == 'Bruce Banner': return "Hulk" elif realName == 'Clint': return "Hawkeye" elif realName == 'Natasha': return "Black Widow" else: return "Not in record"
Now you import this file into another file,
import SuperHero print(SuperHero('Natasha'))
This will throw error, module object is not callable.
But why this happened? Because our function name as well as filename is same, i.e.
SuperHero. Now Python has imported the file or, you can say module. Modules are not callable; functions are. To correctly implement this code, we need to make few changes –
import SuperHero print(SuperHero.SuperHero('Natasha'))
SuperHero.SuperHero tells the python that we want to access
SuperHero() function from
You can also do the same thing with this approach –
from SuperHero import SuperHero print(SuperHero('Natasha'))
Note: Try to avoid using built-in function names. Here is the list –