typeerror: 'module' object is not callable

The error message "TypeError: 'module' object is not callable " is triggered when confusion arises between a module name and a class name. The issue typically originates from the import statement, where a module is imported instead of a class. This confusion can occur due to identical names between the module and the class, leading to the error.

If you have a class MyClass in a file called MyClass.py , then you should write:

from MyClass import MyClass

How to fix typeerror: 'module' object is not callable

The following Python example shows, a file named MyClass.py contains a class named MyClass. When you import the module "MyClass" into another Python file named sample.py, Python recognizes the imported module as "MyClass," but it doesn't directly perceive the class named "MyClass" declared within that module. This situation can result in confusion when attempting to access the class's attributes or methods in the importing file.

MyClass.py
class MyClass: myVar = 10
sample.py
import MyClass obj = MyClass(); print(obj.myVar);

When you run the sample.py , you will get the following error.

D:\python\dev>python sample.py
Python type error

In Python, a script is essentially a module whose name corresponds to the filename. Thus, initiating your file MyClass.py with the line import MyClass inadvertently creates a circular reference in the module structure. This circularity can lead to complications and should be avoided to ensure proper module functioning.

'module' object is not callable

You can fix this error by change the import statement in the sample.py sample.py

from MyClass import MyClass obj = MyClass(); print(obj.myVar);

Here you can see, when you changed the import statement to from MyClass import MyClass , you will get the error fixed.

How to solve typeerror: 'module' object is not callable

In Python, all entities such as functions, methods, modules, and classes are objects, and methods are akin to other attributes. Consequently, there exists no distinct namespaces exclusively for methods. Therefore, when an instance attribute is established, it supersedes a class attribute sharing the same name. To circumvent this, a straightforward remedy is to assign distinct names to attributes.

Conclusion

The "TypeError: 'module' object is not callable " error arises when you mistakenly attempt to call a module as if it were a function or callable object. This often occurs due to confusion between a module's name and the name of a class or function within it, leading to improper usage.