Python supports various kinds of standard modules such as math, sys, os, random, etc. It also supports open-source modules to download and install in Python, such as OpenCV, Keras, Tensorflow, etc. All these modules provide various functions accessed after importing these modules at the program’s start.
The error “TypeError ” appears when functions are used directly without mentioning their module. This write-up will provide various reasons and solutions to fix the “module object is not callable” error in Python. This Python guide will discuss the contents listed below:
- Reason: Calling Module as a Function
- Solution: Call Module Function Using Dot Notation
- Reason 2: Accessing Custom Module Incorrectly
- Solution: Use Dot Notation Syntax to Access Function of Custom Module
Reason: Calling Module as a Function
The main reason which causes this error is when a user tries to call an imported “module” as a function in Python.
An example of this is shown in the below snippet:
The above snippet shows that the “TypeError: module object is not callable” in Python occurs when the “math” module is called a function.
Solution: Call Module Function Using Dot Notation
To resolve this error, the module’s functions must be called using the dot notation. Math modules provide various functions, such as tan(), pow(), exp(), etc., that are used to achieve different functionalities. To access these functions, users must call these functions of the “math” module using the dot notation.
import math num = 2 num1 = 5 print(math.pow(num, num1))
In the above code, The “math” module is imported at the start of the program, and the function “math.pow()” is used to find the power of given input numbers.
The “math.pow()” function successfully calculates the power of the input number using dot notation.
Reason 2: Accessing Custom Module Incorrectly
This error occurs when a custom module is accessed incorrectly in a Python program. For example, we have created a custom function in Python and saved it in the current working directory of Python.
Code: (Created Custom Function)
def find(num, num1): return num + num1
In the above code, the user-defined function named “find” is created that will return the sum of two input variables. The program with a user-defined function is saved as a Python file named “calculation.py”. It consists of the following properties:
- Module Name: calculation.py
- Function Name: find()
Now we have created a new program and imported the calculation module to access the user-defined “find()” function.
An example of this is shown below:
The above snippet shows “TypeError” because the “calculation” module is accessed as a function.
Solution: Use Dot Notation Syntax to Access Function of Custom Module
To resolve this error, the custom module function “find()” is accessed using the dot notation. Such as “calculation.find()”.
import calculation print(calculation.find(4, 5))
In the above code, the “calculation” module is imported using the “import” keyword. The user-defined function of the calculation module is accessed using the dot syntax.
The above output shows the appropriate result, which proves that the user-defined function “calculation.find()” is working properly.
The “module object is not callable” error arises when a user tries to call an imported module as a class or function in Python. This error also occurs when we import a “user-defined” function into any other Python program. To resolve this error, the user-defined function of that particular module must be accessed with “dot notation” syntax. This Python guide explained various causes and solutions of the “module object is not callable” error in Python.