Python is well known for the different modules it provides to make our tasks easier. Not just that, we can even make our own modules as well., and in case you don’t know, any Python file with a .py extension can act like a module in Python.
In this article, we will discuss the error called “typeerror ‘module’ object is not callable” which usually occurs while working with modules in Python. Let us discuss why this error exactly occurs and how to resolve it.
Fixing the typeerror module object is not callable error in Python
Since Python supports modular programming, we can divide code into different files to make the programs organized and less complex. These files are nothing but modules that contain variables and functions which can either be pre-defined or written by us. Now, in order to use these modules successfully in programs, we must import them carefully. Otherwise, you will run into the “typeerror ‘module’ object is not callable” error. Also, note that this usually happens when you import any kind of module as a function. Let us understand this with the help of a few examples.
Example 1: Using an in-built module
Look at the example below wherein we are importing the Python time module which further contains the time() function. But when we run the program, we get the typeerror. This happens because we are directly calling the time module and using the time() function without referring to the module which contains it.
Python3
import time inst = time() print (inst) |
Output
TypeError Traceback (most recent call last) Input In [1], in <module> 1 import time ----> 2 inst = time() 3 print(inst) TypeError: 'module' object is not callable
From this, we can infer that modules might contain one or multiple functions, and thus, it is important to mention the exact function that we want to use. If we don’t mention the exact function, Python gets confused and ends up giving this error.
Here is how you should be calling the module to get the correct answer:
Python3
from time import time inst = time() print (inst) |
Output
1668661030.3790345
You can also use the dot operator to do the same as shown below-
Python3
import time inst = time.time() print (inst) |
Output
1668661346.5753343
Example 2: Using a custom module
Previously, we used an in-built module. Now let us make a custom module and try importing it. Let us define a module named Product to multiply two numbers. To do that, write the following code and save the file as Product.py.
Python3
def Product(x,y): return x * y |
Now let us create a program where we have to call the Product module to multiply two numbers.
Python3
import Product x = int ( input ( "Enter the cost: " )) y = int ( input ( "Enter the price: " )) print (Product(a,b)) |
Output
Enter first number: 5 Enter second number: 10 Traceback (most recent call last): File "demo.py", line 6, in <module> print(Product(a, b)) TypeError: 'module' object is not callable
Why this error occurs this time?
Well again, Python gets confused because the module as well as the function, both have the same name. When we import the module Product, Python does not know if it has to bring the module or the function. Here is the code to solve this issue:
Python3
from Product import Product x = int ( input ( "Enter the cost: " )) y = int ( input ( "Enter the price: " )) print (Product(a,b)) |
Output
Enter the cost: 5 Enter the price: 10 50
We can also use the dot operator to solve this issue.
Python3
import Product x = int ( input ( "Enter the cost: " )) y = int ( input ( "Enter the price: " )) print (Product.Product(a,b)) |
Output
Enter the cost: 5 Enter the price: 10 50