The TypeError: ‘module’ object is not callable error occurs when python is confused between the class object and module. Python attempts to invoke a module as an instance of a class or as a function. This TypeError: ‘module’ object is not callable error occurs when class and module have the same name. The import statement imports the module name not the class name. Hence either the module object is not callable or the class object is not callable in python.

If a python file has the same name in both module name and class name, The import command imports the module name. When you try to access as a class instance, either the module is not callable or the class is not callable. Therefore, the error TypeError: ‘module’ object is not callable will be thrown.

The python module name is just a python file with the .py extension. The module is loaded with from keyword. The class in the file will be loaded using the import keyword. If both the module and class name are same and import statement is created without from keyword, then the python import would recognizes as a module name instead of the class name.



Condition for TypeError: ‘module’ object is not callable

There are three conditions that must be met for this error.

  • The python file name should be the same as that of python class name
  • The python class name should be imported using the keyword import
  • The python file is not loaded using the from keyword


Exception

The python interpreter will display the type error similar to the following

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    emp = Employee();
TypeError: 'module' object is not callable
[Finished in 0.1s with exit code 1]


Root Cause

If the name of the module is same as the class name and the import command is used to load the class without using from keyword for module reference, the python loads module object using import statement. If you try to access as a class instance, then the error TypeError: ‘module’ object is not callable will be thrown.

The code is written to invoke as a class or function but is defined as a module object by the python interpreter. The python interpreter throws this error TypeError: ‘module’ object is not callable, as it can not handle class methods within the module object.



How to reproduce this issue

The TypeError: ‘module’ object is not callable error occurs when the python file is available in the python path and the file name is included in the import statements. If the python imports a class as the name of the python file, this error can be reproduced.

The following example will create an error called “TypeError: ‘module’ object is not callable” as the name of the Employee class is the same as the name of the Employee.py file. The Employee class is imported into test.py. The python interpreter detects the Employee as a module and throws an error when the class object is created.

Employee.py

class Employee:
	id = 1

test.py

import Employee

emp = Employee();
print(emp.id);

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    emp = Employee();
TypeError: 'module' object is not callable
[Finished in 0.1s with exit code 1]


Solution 1

The python module should be loaded using the “from” keyword. If the module is loaded before the class is imported, the error “TypeError: ‘module’ object is not callable” will be resolved. The example below demonstrates how to load the module using the keyword “from.”

In the example below, Employee class is created in Employee.py python file. The instance of the Employee class is created in the test.py python file. If you import the Employee class, python interpreter identifies as you are importing Employee.py python file instead of Employee class. Therefore, you cannot call Employee() constructor as module does not have constructor.

Employee.py

class Employee:
	id = 1

test.py

from Employee import Employee

emp = Employee();
print(emp.id);

Output

1


Solution 2

The python detects the import as a module object. Therefore the name of the class is referenced along with the name of the module. The module name is prefixed with the name of the class. The python interpreter will use the module name to define the class, as shown in the example below.

Employee.py

class Employee:
	id = 1

test.py

import Employee

emp = Employee .Employee();
print(emp.id);

Output

1


Solution 3

The name of the python file and the name of the python class should be created differently so that the error can be easily detected. For example, the name of the python file should be created with a lower case. The python class has been created with the camel case. This error “TypeError: ‘module’ object is not callable” is quickly recognized and helps to prevent this error.

In the example below, the file name is created with lower case “employee.py”. The class is created with camel case “Employee”.

employee.py

class Employee:
	id = 1

test.py

from employee import Employee

emp = Employee();
print(emp.id);

Output

1



Leave a Reply