Typeerror module object is not callable error occurs while invoking any module in the place of python functions. Python modules may contain classes, functions, and member variables. If while calling any function we mistakenly swap modules and functions and by mistake, we call any module in the place of the function, we get this error.
Typeerror module object is not callable (Cause):
We have already seen the generic cause for this error “module object is not callable”. In this section, we will address different cases for this-
Case 1: Invoking 3rd party Modules as Function-
As the above image shows. Numpy is a python module, not a python class. Hence We can not create an instance of it. That’s why the python interpreter throws the above error.
Case 2: Invoking custom module as function –
This case is more often than the above one. We usually create a module and a member class with the same name. Suppose we need to create the object for the class. But unfortunately, we invoke the module as a constructor of the class. The below code is self-explainable.
class Functionality: def execute(self): print("welcome")
If we save this module as Fucntionality.py and then import the same into a new python script -script.py ( refer to the below code )
import Functionality obj= Functionality()
Typeerror module object is not callable (Solution):
The Golden rule to fix this error is to invoke the respective python class or function in the place of the python module. This concept is the same for library python modules like NumPy, pandas, etc and custom develop python modules.
Case 1 : (Solved) Invoking 3rd party Modules as Function-
Let’s import the standard time module. Here time module contains the time() function.
import time obj=time() print(obj)
But in the above code, it directly calls the module. Which is not the right way. That’s why it was through the same error.
The correct way is below.
Another way to fix this below. Below we have imported the respective function time() from the complete module. Earlier we were importing the complete module. Which provides the double reference of the same name to the python interpreter. That is why it was through the same error.
import time obj=time.time() print(obj)
Case 2 : (Solved) Invoking custom module as function –
In addition, Suppose we create a python module with the name my_utillity.py which has the function my_utillity(). Here if we import the complete package call the my_utillity() directly. It will show the same error. The best way to fix the module object is not callable is already mentioned above. It will remain unchanged.
Here is my_utillity.py which contains the my_utillity() function.
def my_utility(): return "My utility invoked"
The correct way to call it.
from my_utility import my_utility print(my_utility())
import my_utility print(my_utility.my_utility())
To sum up, I hope now you have understood the root cause of this error with its Fix. Please comment below if you have any related views over it.
Related Articles ( Must Read for you ):
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.