Select Page

How to Solve Python TypeError: ‘module’ object is not callable

by | Programming, Python, Tips

If you want to access a function belonging to a Python module, you have to specify the module within your program where the function resides. You can import modules using the import statement. If you try to call a module with the intent of calling a class, you will raise the error: TypeError: ‘module’ object is not callable.

This tutorial will go through the error in detail and an example scenario to solve the error.


TypeError: ‘module’ object is not callable

What is a TypeError?

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type. In this case, trying to call a Python module is not possible.

What is a module?

Modules are a vital part of Python that enable you to define functions, variables, and classes outside of your main program, which you can import. Modularizing your code allows you to categorize code blocks, making software development efficient. Any Python file is a module if it ends in the extension “.py“. You can use any Python source file as a module by executing an import state in another Python program. The syntax for import is as follows:

import module_1

You can import more than one module using the following syntax:

import module_1, module_2, module_3, ... module_N

You can also use the from… import statement to import specific attributes like a class from a module into your program. The from… import has the following syntax:

from module_name import name_1

You can import more than one attribute using the following syntax:

from module_name import name_1, name_2, name_3, ... name_N

If you want to import all attributes from a module, you can use the following statement

from module_name import *

If you try to call a module as if it were a function, for example:

module_name()

then you’ll raise the error: TypeError: ‘module’ object is not callable.

Example

Let’s define a module called electron that stores a class, whose attributes are the physical constants of the electron particle. We will write the class in a file called electron.py. Let’s look at the code:

class electron:

    def __init__(self):

        self.charge = -1

        self.mass = 0.51

        self.spin = 1/2

    def positron(self):

         self.charge = +1

         return self.charge

    def get_mass(self):

        return self.mass

The class defines the variables charge, mass, and spin, and two functions, positron() and get_mass(). Next, we will open a file called particle_test.py and write the following code:

import electron

ele = electron()

print(f'Electron charge = {ele.charge}')

print(f'Electron mass = {ele.mass} MeV')

The above code imports the electron module and attempts to use the electron module to get the charge and mass of the electron and print it to the console. Let’s run the code using the command python particle_test.py to see what happens:

Traceback (most recent call last):
  File "particle_test.py", line 3, in module
    ele = electron()
TypeError: 'module' object is not callable

The error occurs because we import the module but do not specify which attribute to use from the module.

Solution

We can solve this error in two ways. First, we can call the name of the class we want to reference instead of the function itself. In this case, the module and the class have the same name. Let’s look at the revised code:

import electron

ele = electron.electron()

print(f'Electron charge = {ele.charge}')

print(f'Electron mass = {ele.mass} MeV')                                          

In the above code, we create an instance of the electron class, which gives us access to its attributes. We then print the charge and the mass values to the console. Let’s run the code to see what happens:

Electron charge = -1
Electron mass = 0.51 MeV

Second, we can also the from… import statement to specify the class to import. Let’s look at the revised code:

from electron import electron

ele = electron()

print(f'Electron charge = {ele.charge}')

print(f'Electron mass = {ele.mass} MeV')

In the above code, we are still creating an instance of the electron class; we import the class directly into the program with the from… import statement. Let’s run the code to see what happens:

Electron charge = -1
Electron mass = 0.51 MeV

Both solutions result in the program successfully creating an electron object then printing the charge and mass values to the console.

Summary

Congratulations on reading to the end of this tutorial. The error “TypeError: ‘module’ object is not callable” occurs when you try to call a module as if it were a function. When working with modules, ensure that you correctly reference the functions, variables, and classes you want to access. If you’re going to call a function belonging to a module, you have to specify that function instead of calling the module.

For more reading on the ‘not callable’ TypeError, go to the article: How to Solve Python TypeError: ‘list’ object is not callable.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!