Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] [Solved] TypeError: ‘Module’ Object Is Not Callable in Python

#1
[Solved] TypeError: ‘Module’ Object Is Not Callable in Python

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 concatenate str (string) objects. But in the case of int(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 to str 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 modulesolve’ 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

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

The Complete Guide to PyCharm

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

The post [Solved] TypeError: ‘Module’ Object Is Not Callable in Python first appeared on Finxter.



https://www.sickgaming.net/blog/2021/03/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Determine the Type of an Object in Python? xSicKxBot 0 24 04-06-2021, 07:26 PM
Last Post: xSicKxBot
  [Tut] [Solved] NumPy RuntimeWarning: All-NaN slice encountered xSicKxBot 0 17 04-02-2021, 11:10 AM
Last Post: xSicKxBot
  [Tut] [Solved] TypeError: ‘int’ Object Is Not Subscriptable in Python xSicKxBot 0 24 03-31-2021, 08:56 PM
Last Post: xSicKxBot
  [Tut] Python Celery – How to Get a Task Result Object by ID? xSicKxBot 0 17 03-18-2021, 06:52 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 67 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] How to Check if an Object is of Type List in Python? xSicKxBot 0 53 02-09-2021, 08:05 AM
Last Post: xSicKxBot
  [Tut] How to Fix “TypeError: len() of unsized object” xSicKxBot 0 62 01-31-2021, 12:55 PM
Last Post: xSicKxBot
  [Tut] Python help() xSicKxBot 0 65 01-22-2021, 01:02 AM
Last Post: xSicKxBot
  [Tut] How to Get The Current Reference Count of an Object in Python? xSicKxBot 0 67 12-21-2020, 08:13 AM
Last Post: xSicKxBot
  [Tut] Python complex() — A Useless Python Feature? xSicKxBot 0 73 12-19-2020, 02:05 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
Unigine 2.14 Released
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 2
News - Lawson Will Offer New Pokémon Sna...
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 1
News - Nacon Announces WRC 10, Speeds On...
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 1
News - Blizzard Arcade Collection Gets T...
Last Post: xSicKxBot
Today 03:06 PM
» Replies: 0
» Views: 2
(Indie Deal) FREE Stranded In Time | XCO...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 8
Microsoft - 5 reasons to attend Azure St...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 7
News - Deals: Ring Fit Adventure Is Down...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 8
News - Cuphead Will Have An Awesome Deca...
Last Post: xSicKxBot
Today 07:54 AM
» Replies: 0
» Views: 8
News - Nintendo Actually Responded To A ...
Last Post: xSicKxBot
Today 01:50 AM
» Replies: 0
» Views: 9
News - Rumour: Dragon Ball Z: Kakarot Co...
Last Post: xSicKxBot
Today 01:50 AM
» Replies: 0
» Views: 9

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020