There are many in-built functions in Python, offering a variety of operations. All these functions are within modules or libraries. So, if you want to use a function that is within a module, you need to specify the module within the program where the function resides. Modules are included in Python programs using the import statement. For example,
This statement will include the math module within your program. You can use the functions such as factorial(), floor() and fabs() within this module. But if you try using a function with name math(), the compiler will be confused. It will throw an error called TypeError ‘module’ object is not callable in Python.
Here, we will focus on a solution to this problem.
In Python, all inbuilt function is provided by modules, therefore to use any function within the module, we need to include that module in our code file.
Note: Module is the collection of code library in a categorical manner.
What is TypeError ‘module’ object is not callable in Python
This error statement TypeError: ‘module’ object is not callable occurs when the user gets confused between Class name and Module name. The issue occurs in the import line while importing a module as module name and class name have the same name.
Cause of this Error
The error “TypeError: ‘module’ object is not callable” occurs when the python compiler gets confused between function name and module name and try to run a module name as a function.
# Import os Module import os os()
Traceback (most recent call last): File "call.py", line 4, in <module> os() TypeError: 'module' object is not callable
In the above example, we have imported the module “os” and then try to run the same “os” module name as a function.
And as we can see In the module “os” there is no function with the name “os” therefore “TypeError: ‘module’ object is not callable” is thrown.
Example with Custom module and function
To explain this error, we will create a module and function with the same name.
File name: mymodule.py
def mymodule(): myval='STechies' print(myval)
In the above code we have created a file name “mymodule.py” and in that file created a function with the name “mymodule”
We can see that the module name and the function name in the above code is the same.
File name: mycode.py
import mymodule print(mymodule())
Traceback (most recent call last): File "mycode.py", line 3, in <module> print(mymodule()) TypeError: 'module' object is not callable
In the above code, we are trying to call a function named “mymodule” which is in module “mymodule”, due to the similar name of module and function and due to this our python compiler gets confused and throws the following error
“TypeError: ‘module’ object is not callable”
How to fix typeerror: ‘module’ object is not callable?
To fix this error, we need to change the import statement in “mycode.py” file and specify a specific function in our import statement.
from mymodule import mymodule print(mymodule())
In the above code, we have imported “mymodule” function from “mymodule” module, so our compiler won’t get confused and can execute the function mymodule().