TypeError module object is not callable [Solved]

The TypeError module object is not callable error occurs when we tried to import a module using the import keyword and then call it as a function or class. The error can be easily solved by using dot notation to access the specific function or class before calling it. In this short article, we will discuss how we can solve TypeError module object is not callable error using various methods. Moreover, we will also learn how we properly import a module and access its function.

Solve TypeError module object is not callable

The TypeError module object is not callable error is caused by incorrectly calling the function of the module. The error can be easily solved using the dot operation. For example, if a module has a function named myFunction then we can get access to the function by first calling the module followed by the dot operator and then the name of the function.

module.myFuction()

Let us see why the error TypeError module object is not callable occurs and how to solve it. Let us assume that we imported the math module and want to print it:

# importing the module
import math

# printing the module
print(math())

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_156831/2690687042.py in <module>
      3 
      4 # printing the module
----> 5 print(math())

TypeError: 'module' object is not callable

As you can see, we get the error because we are treating the module as a function. In Python, we used round brackets after calling a function and when we use the same round brackets after calling a module, we get the error.

Remove Round Brackets

If you want to print the module, then you need to remove the round brackets because round brackets indicate that it is a function. So, the correct way of printing a module is as follows:

# importing the module
import math

# printing the module
print(math)

Output:

<module 'math' (built-in)>

As you can see, we got the output without getting any errors.

Accessing the Functions of the Module

In order to access the functions of the module, we used the name of the module followed by the dot and the name of the function. Let us assume that we want to find the square root of a number using the math module. Then the following is the correct way of calling the module and its function:

# importing the module
import math

# printing square of the number
print(math.sqrt(9))

Output:

3.0

As you can see, we didn’t get TypeError module object is not callable error because we are correctly calling the function. Notice that we have used the round brackets after the name of the function rather than the name of the module:

Another way to import the module is using the as keyword as shown below:

# importing the module
import math as m

# printing square of the number
print(m.sqrt(9))

Output:

3.0

As you can see, we solve the problem.

Another way is to directly import the function from the module using from keyword. In this case, we don’t need to use the name of the module and the dot operator, we can directly use the name of the function as shown below:

# importing the module
from math import sqrt

# printing square of the number
print(sqrt(9))

Output:

3.0

Notice that we were able to get access to the function of the module without getting TypeError module object is not a callable error.

Alternative Solutions

Let us assume that you have a Python module named MyClass.py and the file is located in the same directory. The module has the following class:

class MyClass:
    ....
    ....

If you are using the following import method to import the class, you will get TypeError module object is not a callable error.

# importing the class
from MyClassParentDir import MyClass

This will raise the error TypeError module object is not callable. Instead, we can use the following correct way of importing classes from the module:

from MyClassParentDir.MyClass import MyClass

This will help to get rid of the issue:

Understanding the TypeError module object is not callable error

It is very easy to understand and interpret errors in Python. Mainly, there are two parts of Python errors. The first part shows the category of the error while the second part gives more description of the error. For example, in our case, the error belongs to the TypeError category. While the second part explains what kind of type error we are getting. It clearly says we are trying to call the module object which is not actually callable in Python.

Reasons for getting ‘module’ object is not callable error:

  • Incorrectly importing the function from the module.
  • Calling the module instead of function.
  • Calling the module instead of class.
  • Using round brackets around the module name.

Conclusion

The error occurs when we tried to call a module that is not callable. In fact, the modules are not callable. We need to import the functions from the module and call them. In this short article, we discussed how to solve TypeError module object is not callable error using various methods. We discussed the reasons for getting TypeError module object is not a callable error and solved the error using 3 different methods.

Related Errors

Leave a Comment