Python

Python Object is Not Callable Error

When you mix up the class and module names, Python returns a TypeError:’module’ object that is not callable. While coding, this might happen for many reasons. To comprehend what “object is not callable” means, we must first comprehend what a Python callable is. As the title indicates, a callable object is something that could be called. Simply use the built-in method callable() and send it an object to see if it is callable.

Have you ever realized that while you’re running a Python code, the TypeError object isn’t accessible? We will work together to discover why this occurs. When an object that is not callable is called using parentheses (), the Python interpreter raises the “TypeError” i.e., the object is not a callable error. This can arise if you accidentally use parenthesis () rather than square brackets [] to retrieve elements of a list. We will show you some scenarios where this error occurs, as well as what you can do to fix it. Let’s look for the problem! But, what does it mean when an object isn’t callable?

When you call a module while coding, this might occur for many reasons. Most commons are when you call an object rather than a Class or Function within that module, you will get this error. Let’s have a look at each case and how to resolve “the’module’object” is not a callable problem.

Example 1:

In our first example, we will use a function to invoke a built-in Python module. The code below shows how to import a socket module in Python, and how to use it as a function afterward. Because we are using the similar name for the module and executing the “socket” module as a method, Python will raise the “TypeError:’module’ object is not callable”.

import socket

a = socket(socket.AF_INET, socket.SOCK_STREAM)

print(a)

https://lh5.googleusercontent.com/i6rWc8iuxNibZx0B7mT7lOHVcV_FEEyMhdmG4uBLXK2ORbD5TEW5FzdVYVoMl9d6lCgdM1ojyhr1Px8ddSvALQ-wuK074EMzE1iIwPf-oVw8zSHP0SzabJ6H3X_KSiFwemiFsf57

Here is the result screen that shows the generated error. It is most common among developers, who get confused between module names and class names.

https://lh6.googleusercontent.com/ik25daTmzEAbGM6xNPqr4PqTTACZUM9sB4H4w09rxwnjgTGZjkvW6HR0zkvX9TXRz3NPIW2wHGA3TIp_WIVPuNETPJTuHS0MnL59mBYVkZV4Rbf5igzuInwMSwcr29mbS0t_8OZ3

Here are some solutions that can apply. The first solution is to call the function with **Modulename instead of calling the module name directly. Inside the module, there is a function named “FunctionName”.

import socket

a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print(a)

https://lh3.googleusercontent.com/gaI6HdY3roJP9KUlHeHaumzd5P5vlSs1U2gUp3Wc6fBHVuYSO9F-uE-FB7S3Uzi_VvgmuBgwYTKhHj4dTCcUH7iQ55MO-1F2pER0LePUDLwYUg0JHhe0rDhHD1Gk-V9UIiCztyNx

Here is the result. As you can observe, the code was successfully executed and no errors were generated.

https://lh4.googleusercontent.com/rMXAtSK7zFsOVKK1erujFLS43H2BsKAywDaD2tcwNl-NIzhzS5B8Jaed3F_QdrvIyzrjKzYG3QXqVNwtkYGATguzcYjUj_JaHOIkCenYKn-cWMoe-VSZey70u1r-qrnyyNYAe3gC

Altering the import statement as shown below, is another option. While executing the code, the compiler will not be confused between the module and function names.

As you can see, the code was successfully executed and no errors were generated.

from socket import *

a = socket(AF_INET, SOCK_STREAM)

print(a)

https://lh5.googleusercontent.com/tLO9ir8rZYKq-09xIjOGP_IODorvIyKjYPj4ZcmPgFINkkEFcP1S7_0JY16jP53Ckd80MfF4oJIxzAHFEfIw4AV0hqCir4yBYrj2dMpeIKISEuYifplv32xKjUyAHuHxJG8L9rs3

Here, you can see the successful execution of the above code.

https://lh4.googleusercontent.com/glICo4CVgLHNQEMGvL61M1HH8Nhx4HI7VpMMDrcq2riYZXyevpdIOcop60nQxBVFf7dGAdWf3Qsf55T8Nvst8GZXADx4Vq-kIrNSmYG2Loctvo7bXTUOlvdH21FFMU5wlSIGURgw

Example 2:

Another example is having a custom module named ” mymodule ” and utilizing it as a function, which results in a TypeError. We have built a file called “namemodule.py” in the example below.

def mymodule();

   n= ‘Python is easy to learn’

   print()

https://lh6.googleusercontent.com/_bcIS6M6wmrWrh4KJHmCeX8DEDk22sWk4vP-AtvC64POywH7GomMIhhwx87IiJ1epjGju9Fd_69sk1xmJy58xXHIOBPA1w5D0YXJm1jmTatcOwAdL02seManWw9fygak7LNN7DAR

We try to import the mymodule and call it a function in the second step, which results in a TypeError.

import mymodule

print (mymodule())

https://lh4.googleusercontent.com/aL_K-lLMQBcR9nS_xuIJCPBD5Jo1BoCAnCepsJhZKLD8vjJA7wHo6bNg67QFTcJCQ4ioIK5R2h70eqDfJHQCgWiqzniQ15SIUrJiS4DPOntUfiVD8HSg3RvAQL88X09WHyFYoizU

Executing the above code generates an error as you can see in the attached screenshot.

https://lh5.googleusercontent.com/j9AZiZCQarRGBiQ85Qp28LooXb4UVkmP4QFefY-XWU3pfx9ml2yyi8gq9rIhltazEK3ZAV8Up4FgwHWjhGAYTLiXJC7BjdEPY7ZxmBcXvp7Rae5yg9yBEr-Le_tEpkwQGU4sEr-m

Here is the best solution that you can apply when facing a similar situation. So, rather than importing the module, one can import the function or feature within the module, as shown below, to avoid the error.

from mymodule import mymodule

print (mymodule())

https://lh4.googleusercontent.com/b17Omwz3eH-QDNPNz5BVh1lKA4ukTK1xOlr2quWlF2VdSa6j2ucLe9ffx7_vZ1X1KCs-IWMYywo8ay8QYyqIwXbd4TMiCxWtZpoE2FfDgeU7G7OOhuTfdSyckGSIvuGwhZBGV0hv

Here, you can see that after applying the above solution, the code is perfectly executed and displayed the following output.

https://lh6.googleusercontent.com/BJwH_R8rf8A26jZNOOaur-lLv44afcdbFxxi8UsBHYN33IvsduveMu2KCZkDN6qmzIrwlWw33MFi89hhsNchNeE6yuQxd-OYrBu-PDU1l2V99c1Durm2k3ZUX2ESazsx7JiMAl-e

Example 3:

To convert a value to a numerical value, use the int() function. The int() method returns an integer object made up of a number or a string x, or 0 if no parameters are provided. To be transformed into an integer object, a number or string must be provided. The value of the default parameter is zero.

int = 5

b = int(input('Enter value: '))

for i in range(1, int):

print(i * 5)

https://lh3.googleusercontent.com/CwToIMjE_PD3oEBscX-PXnLNBUPy-cwn4WW3IjbH1BaQmvF-gE1eokRZWKLovYFZuG9ARHu_IeyqeRTUF4xRfLv6YJ11l_y1PW_nmqAK-6i4AldHtUyibM-ql2csWWxlzSh7hpbq

Below, you can see the result. As you can see, that it generates an error.

https://lh4.googleusercontent.com/P_p3lk5Qdv6XWyImQbw6zarTvnxniCiv8TDFqnDBjN-IuweY6A9Kr1eLYsZsTomkGHhVAIPq-oXUEjmBGOar6w329_hYNIrV-jiypjx8kUHbpcXpCcxBwjShXwHU0iZv-OpXrRPp

You can overcome this problem by giving the variable a different name. Refer to the below code.

a = 5

b = int(input('Enter value: '))

for i in range(1, a):

print(i * 5)

https://lh4.googleusercontent.com/SY3RrCBbj0JHTA4-RxgFzejwhAgdC2t5DUg5Kano0c-f0pLJVvwQlzBmhS_UJ7wbdjr9Pn2xBcd2lZcL29uPD74pvhCJ8qDaoDZRqIWh6qjOS23V8-18EHcY60YSfM_tgoU-1Lar

Here, the changes in the code produce the correct output as you can see below.

https://lh6.googleusercontent.com/pubvHscEPQoj2KHFn2AUXa_XwNGTTq6OAuIOI_Qt4457Mbk--hE1j0z6GycL_sgWNvm6Y5JV53vPr8WNn8ZyW2GG58ulhnNeqcYX_Lh7iLzRAUmxF-vh8wTk2vOlUiWqTN6M80OM

Conclusion:

When a certain operation is performed on an object that has the wrong type, a TypeError is thrown. When you try to access a module as a function in your program, you’ll get a “TypeError:’module’ object is not callable” error. This arises when you get misled between the name of a module and the name of a class or method within that module. If you try to use the + operator to combine a string and an integer object, you’ll get a TypeError since the + operation isn’t allowed between objects of different types. In this post, we have shed light on the “TypeError: ‘Module’ Object Is Not Callable” and how to fix it in your Python programs.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content

Page was generated in 0.060405015945435