TypeError: 'module' object is not callable in Python

TypeError: 'module' object is not callable in Python

Introduction

In Python, an error "TypeError: 'module' object is not callable" is raised when an object of the incorrect type is used in a function call. A common cause of this error is when you have a module and a function with the same name, leading to confusion as to which one you are trying to call. This tutorial will help you understand and resolve this error in your code.

Problem

When trying to call a module in your code as if it were a function, you may encounter an error with the message "TypeError: 'module' object is not callable". This indicates that you are trying to call the module itself, instead of a function or attribute within the module. For example:

import math
result = math()

This will raise the following error:

TypeError: 'module' object is not callable.

Solution for TypeError: 'module' object is not callable

To resolve this error, you need to call a function or attribute within the module instead of the module itself. For example, using the sqrt function from the math module, you would do the following:

import math

result = math.sqrt(4)
print(result)  # Output: 2.0

If you have defined a function with the same name as a module you have imported, you should rename your function to avoid conflict.

import my_module

def my_function():
    print("Hello from my function!")

result = my_function()
print(result)  # Output: Hello from my function!

Another Example:

import datetime

def my_datetime():
    return datetime.datetime.now()

result = my_datetime()
print(result)  # Output: 2023-02-04 14:00:00.000000

The code defines a function named my_datetime which uses the datetime module from the Python Standard Library. This prevents the error "TypeError: 'module' object is not callable" because the function my_datetime is not the same as the module datetime, so there is no confusion as to which one you are trying to call.

When the function my_datetime is called, it returns the result of calling datetime.datetime.now(), which is the current date and time. The function is not being called as if it were a module, so the error is not raised.

Python program outputs current date and time.

See also: How to fix "TypeError: builtin_function_or_method object is not subscriptable" in Python?

Conclusion

In conclusion, the "TypeError: 'module' object is not callable" error occurs when you try to call a module as if it were a function. To resolve this error, you need to call a function or attribute within the module instead. If you have a function with the same name as a module you have imported, you should rename your function to avoid conflict.

FAQs

Can the "TypeError: 'module' object is not callable" error be raised with a custom module?

Yes, the error "TypeError: 'module' object is not callable" can also be raised with a custom module that you have created. The same rules apply: if you try to call a module as if it were a function, you will get this error. To resolve it, you need to call a function or attribute within the module instead of the module itself.

What should I do if I have a function with the same name as a module I have imported?

If you have a function with the same name as a module you have imported, you should rename your function to avoid conflict. This will prevent the error "TypeError: 'module' object is not callable" from being raised when you try to call your function.

Can I call a module's function and avoid the "TypeError: 'module' object is not callable" error?

Yes, you can call a module's function and avoid the "TypeError: 'module' object is not callable" error. To do this, you need to import the module and then call its functions using the module name as a prefix, for example: module.function(). This ensures that the correct object is called and the error is not raised. Additionally, you can create a function that returns the result of calling the module's function, which can help to simplify your code and improve readability.