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

TypeError: ‘Module’ Object Is Not Callable in Python

Problem Formulation

Aim:

In this article we will discuss: TypeError: 'Module' Object Is Not Callable in Python and the methods to overcome such errors in your program.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Example:

Consider that you have created the following module with the name Palindrome.py :

Now, you want to use this module in another program and check if an entered value is Palindrome or not :

Let’s execute this code and check the output:

Wait!!! 😵 Why did we get this error? It brings us to a number of questions:

  • What is a TypeError?
  • What does TypeError: module object is not callable mean?
  • Why did we encounter this error in our program?
  • How to fix TypeError: module object is not callable ?

Let us dive into each question in details and find out the correct way of calling a certain module and its attributes.

◈ What is TypeError in Python?

TypeError is raised when a certain operation is applied to an object of an incorrect type. For example, if you try to add a string object and an integer object using the + operator, then you will encounter a TypeError because the + operation is not allowed between the two objects that are f different types.

Example:

Output:

☞ There can be numerous reasons that lead to the occurrence of TypeError. Some of these reasons are:

  • Trying to perform an unsupported operation between two types of objects.
  • Trying to call a non-callable caller.
  • Trying to iterate over a non-iterative identifier.

Now that we have a clear idea about TypeErrors, let us understand what does TypeError: module object is not callable mean?

What Does TypeError: module object is not callable Mean?

TypeError: 'module' object is not callable is generally raised when you are trying to access a module as a function within your program. This happens when you are confused between a module name and a class name/function name within that module. For example:

Output:

In the above example, we have imported the module socket; however, within the program, we are trying to use it as a function that ultimately leads to TypeError: 'module' object is not callable . The user got confused because the module name and the class name are the same.

◈ Therefore we now have an idea about the reason behind the occurrence of TypeError: 'module' object is not callable.So, why did we encounter this error in our example?

The reason is already known to us from the above explanation. If you have a look at the example, you will notice that the name of the module is Palindrome while it has a function with the name Palindrome as well. So, that’s what created the confusion!

That brings us to the all important question. How do we fix the error?

◈ Fix: TypeError: ‘module’ object is not callable

There are couple of ways you can use to fix this error.

Method 1: Change The “import” Statement

Instead of importing the module you can import the function or attribute within the module to avoid the TypeError. Let’s have a look at the solution to our example scenario in the program given below.

Output:

As Python now knows that you are referring to the Palindrome function within the Palindrome Module, hence the program runs without any error.

Solution to the socket program mentioned above:

Output:

Method 2: Use ModuleName.ClassName

Another workaround to our problem is to use ModuleName.ClassName or ModuleName.FunctionName instead of just using the module name which causes confusion and error.

Let’s have a look at the solution to our example scenario in the program given below.

Output:

Explanation: When we use Palindrome.Palindrome(text), it tells Python that we are trying to access the Palindrome method of the Palindrome module; therefore there’s no confusion in this case and the program runs without any error.

Solution to the socket program mentioned above:

Ouput:

Let’s have a look at another variation of this kind of TypeError in Python.

Python TypeError: ‘int’ object is not callable

You will encounter TypeError: 'int' object is not callable mostly in the following scenarios:

Scenario 1: Declaring a Variable Named ‘int

If you declare a variable with the name int and you also have a variable that accepts the user input with the help of the int() method, then the Python compiler is unable to distinguish between the inbuilt int() method and the declared variable int in the program.

Example:

Output:

Solution: Use a different name for the variable

Output:

Scenario 2: Declaring a Variable With a Name Of Function That Computes Integer Values

Suppose you have a variable by the name sum in your program and you are also using the built-in function sum() within your program then you will come across the TypeError: 'int' object is not callable since Python won’t be able to distinguish between the two sum variable and the sum() method.

Output:

Solution: Use different variable instead of sum

Output:

Conclusion

Key take-aways from this article:

  • What is TypeError in Python?
  • What Does TypeError: module object is not callable Mean?
  • Fixing the – TypeError: ‘module’ object is not callable
  • Python TypeError: ‘int’ object is not callable

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.