[toc]
Overview
Objective: The purpose of this article is to discuss and fix TypeError: ‘module’ object is not callable in Python. We will use numerous illustrations and methods to solve the issue in a simplified way.
Example 1:
# Example of TypeError:'module' object is not callable import datetime # importing datetime module def tell_date(): # Method for displaying todayβs date return datetime() print(tell_date())
Output:
Traceback (most recent call last): File "D:/PycharmProjects/PythonErrors/rough.py", line 9, in <module> print(tell_date()) File "D:/PycharmProjects/PythonErrors/rough.py", line 6, in tell_date return datetime() TypeError: 'module' object is not callable
Now, the above output leads us to a few questions. Let us have a look at them one by one.
β What is TypeError in Python?
β₯ TypeError
is one of the most common Exceptions in Python. You will come across a TypeError Exception
in Python whenever there is a mismatch in the object types in a specific operation. This generally occurs when programmers use incorrect or unsupported object types in their program.
Example: Let’s see what happens, if we try to concatenate a str
object with an int
object:
# Concatenation of str and int object string = 'Nice' number = 1 print(string + number)
Output:
Traceback (most recent call last): File "D:/PycharmProjects/PythonErrors/rough.py", line 4, in <module> print(string + number) TypeError: can only concatenate str (not "int") to str
Explanation:
In the above example, we can clearly see that the TypeError Exception
occurred because we can only concatenate str
to another str
object and not to any other type of object (e.g. int
, float
, etc
.)
- The β
+
β Operator can concatenatestr
(string) objects. But in the case ofint
(integers), it is used for addition. - If you want to forcefully perform concatenation in the above Example, then you can easily do it by typecasting the
int
object tostr
type.
? Read Here: How To Fix TypeError: List Indices Must Be Integers Or Slices, Not βStrβ?
So, from the previous illustrations, you have a clear idea about TypeError
. But what does the Exception TypeError: 'module' object is not callable
mean?
? TypeError: ‘module’ object is not callable
Python generally provides a message with the raised Exceptions. Thus, TypeError Exception
has a message ‘module’ object is not callable, which means that you are trying to call a module object instead of the class or function object inside that module.
This occurs if you try to call an object thatβs not callable. A callable object can be a class or a method that implements the β__call__β method. The reason for this may be (1) confusion between a module name and a class/function name inside that module or (2) an incorrect class or function call.
β₯ Reason 1: Let us have a look at an example for the first reason, i.e. confusion between a module name and a class/function name.
- Example 2: Consider the following user-defined module –
solve.py
:
# Defining solve Module to add two numbers def solve(a, b): return a + b
Now let us try and import the above user-defined module in our program.
import solve a = int(input('Enter first number: ')) b = int(input('Enter second number: ')) print(solve(a, b))
Output:
Enter first number: 2 Enter second number: 3 Traceback (most recent call last): File "main.py", line 6, in <module> print(solve(a, b)) TypeError: 'module' object is not callable
Explanation: Here, the user got confused between the module name and the function name as both of them are exactly the same, i.e. βsolveβ.
β₯ Reason 2: Now, let us discuss another example that demonstrates the next reason, i.e., an incorrect class or function call.
If we perform an incorrect import or function call operation, then we will likely face the Exception again. Previously in the example given in the overview, we made an incorrect call by calling the datetime
module object instead of the class object, which raised the TypeError : 'module' object is not callable Exception.
Now that we have successfully gone through the reasons that lead to the occurrence of our problem let us find the solutions to overcome it.
? How to fix the TypeError : ‘module’ object is not callable ?
?οΈ Method 1: Changing the βimportβ statement
For fixing the first problem that is confusion between the module name and class/function name, let us reconsider Example 2. Here the module βsolveβ also has a method named βsolveβ, thus creating confusion.
To fix this, we can simply change the import statement by importing the particular function inside that module or by simply importing all classes and methods inside that module.
# importing solve module in Example 2 from solve import solve a = int(input('Enter first number: ')) b = int(input('Enter second number: ')) print(solve(a, b))
Output:
Enter first number: 2 Enter second number: 3 5
?Note:
- Importing all classes and methods is recommended only if the size of the imported module is small as it may affect the time and space complexity of the program.
- You can also use aliasing using a suitable name if you still have a confusion.
- For Example:-
from solve import solve as sol
- For Example:-
?οΈ Method 2: Using The . (Dot) Notation To Access The Classes/Methods
There is another solution to the same problem. You can access the attributes, classes or methods of a module by using the β.β Operator. Therefore you can use the same to fix our problem too.
Let us try it again on our Example 2.
# importing solve module in Example 2 import solve a = int(input('Enter first number: ')) b = int(input('Enter second number: ')) print(solve.solve(a, b))
Output:
Enter first number: 2 Enter second number: 3 5
?οΈ Method 3: Implementing A Proper Class or Function Call
Now let us discuss the solution to the second reason of our problem, i.e. if we perform an incorrect class or function call. Any mistake in implementing a call can cause Errors in the program. Example 1 has exactly the same problem of incorrect function call which raised the Exception.
We can easily fix the problem by replacing the incorrect call statement with the correct one as shown below:
import datetime # importing datetime module def tell_date(): # Method for displaying todayβs date return datetime.date.today() print(tell_date())
Output:
2021-03-24
? Bonus
The above mentioned TypeError
occurs because of numerous reasons. Let us discuss some of these situations which lead to the occurrence of a similar kind of TypeError
.
β¨ TypeError is TypeError: ‘list’ object is not callable
This error occurs when we try to call a βlistβ object and you use β()β instead of using β[]β.
Example:
collection = ['One', 2, 'three'] for i in range(3): print(collection(i)) # incorrect notation
Output:
Traceback (most recent call last): File "D:/PycharmProjects/PythonErrors/rough.py", line 3, in <module> print(collection(i)) # incorrect notation TypeError: 'list' object is not callable
Solution: To fix this problem we need to use the correct process of accessing list elements i.e using β[]β (square brackets). As simple as that! ?
collection = ['One', 2, 'three'] for i in range(3): print(collection[i]) # incorrect notation
Output:
One 2 three
β¨ TypeError: ‘int’ object is not callable
This is another common situation when the user calls upon an int
object and ends up with a TypeError
. You can come across this error in scenarios like the following:
β Declaring a Variable With a Name Of Function That Computes Integer Values
Example:
# sum variable with sum() method Amount = [500, 600, 700] Discount = [100, 200, 300] sum = 10 if sum(Amount) > 5000: print(sum(Amount) - 1000) else: sum = sum(Amount) - sum(Discount) print(sum)
Output:
Traceback (most recent call last): File "D:/PycharmProjects/PythonErrors/rough.py", line 5, in <module> if sum(Amount)>5000: TypeError: 'int' object is not callable
Solution: To fix this problem we can simply use a different name for the variable instead of sum.
#sum variable with sum() method Amount = [500, 600, 700] Discount = [100, 200, 300] k = 10 if sum(Amount)>5000: print(sum(Amount)-1000) else: k = sum(Amount)-sum(Discount) print(k)
Output:
1200
Conclusion
We have finally reached the end of this article. Phew! That was some discussion, and I hope it helped you. Please subscribe and stay tuned for more interesting tutorials.
Thank you Anirban Chatterjee for helping me with this article!
- Do you want to master the most popular Python IDE fast?
- This course will take you from beginner to expert in PyCharm in ~90 minutes.
- For any software developer, it is crucial to master the IDE well, to write, test and debug high-quality code with little effort.
Join the PyCharm Masterclass now, and master PyCharm by tomorrow!