TypeError: ‘module’ object is not callable

The python error “TypeError: ‘module’ object is not callable” occurs when you attempt to invoke a class instance or a function, but the python interpreter is detected as a module. The “import” keyword is used to load a classes and functions. The keyword “from” is used to load a module. But you are importing a module or you skipped loading a module using the keyword “from”.

In python, the module is a simple python files with a .py extension. The name of the module is the name of the file. The keyword “import” is used to load classes and functions. But the python interpreter recognizes the class name or function name as a module name or a python file name.

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

  • A python file should be same as a python class name
  • A python class should be imported using the keyword “import”
  • The python file is not loaded using the keyword “from”

In python, if the name of the module is used as a class name or function name, the python interpreter throws a type error “TypeError: ‘module’ object is not callable”. The python error will look like the one below

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

The keyword “import” is used for loading classes and functions. The “from” keyword is used to load a module. The python module is a simple python file with a.py extension. Python files are created with the name of the class. If you import the class, python will detect that the module is imported into the file.

The error “TypeError: ‘module’ object is not callable” is thrown because the python detects the name of the class as the name of the module. The class functions will throw this error since it can not be executed.



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 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



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *