Sling Academy
Home/Python/Fixing TypeError: ‘module’ object is not callable in Python

Fixing TypeError: ‘module’ object is not callable in Python

Last updated: December 29, 2023

Understanding the Error

The TypeError: 'module' object is not callable occurs in Python when you try to treat a module as if it were a function. This can happen if you import a module instead of a specific function or class from it and then attempt to call it as a function. Python modules are simply files containing Python code that can define functions, classes, or variables. When you import the entire module, you must access its contents using the dot notation.

Common Scenarios and Fixes

A common scenario is confusing module names with the names of functions or classes they define. For example, if there is a file named math.py that defines a function calculate(), importing the module and trying to call it directly would raise the error:

import math
result = math()

To fix this error, you need to call the function defined within the module, not the module itself:

import math
result = math.calculate()

Another fix involves importing the specific function or class from the module:

from math import calculate
result = calculate()

If you’ve mistakenly named your own script with the same name as a standard library module, e.g., random.py, this can also cause the error due to a namespace clash. Rename your script to something unique:

# filename: my_random_calculator.py
from random import randint
result = randint(1, 10)

Complete Code Example

Here is a simple code example that demonstrates defining a module with a function, importing the function, and using it to avoid the ‘module not callable’ error:

# Filename: calculations.py
def add(a, b):
    return a + b

# Filename: main.py
from calculations import add

# Now we can call the add function directly without module name
total = add(3, 5)
print(f'The sum is: {total}')

In this example, the add() function is defined in a separate file named calculations.py. In the main script, only the add function is imported from the calculations module, allowing it to be called directly.

Next Article: Fixing Python AttributeError: ‘NoneType’ object has no attribute ‘xyz’

Previous Article: Fixing AttributeError: ‘str’ object has no attribute ‘read’ in Python

Series: Common Errors in Python and How to Fix Them

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types