Skip to main content

How to Resolve Python Error "TypeError: 'module' object is not callable / subscriptable"

When working with imported modules in Python (like math, json, csv, or your own custom modules), you might encounter errors such as TypeError: 'module' object is not callable or TypeError: 'module' object is not subscriptable. These errors indicate a fundamental misunderstanding of how to access the contents (functions, classes, variables) within an imported module.

This guide explains why modules themselves cannot be called like functions or indexed like lists/dictionaries, and provides the correct ways to access their members.

Understanding Python Modules​

A Python module is essentially a file containing Python definitions and statements (like functions, classes, and variables). When you use import my_module, Python executes the code in my_module.py (if it hasn't already) and creates a module object. This module object acts as a namespace – a container holding all the names (functions, classes, variables) defined within that file.

Crucially, the module object itself is not a function you can call directly with (), nor is it a sequence or mapping you can access with square brackets []. You need to access the specific items inside the module namespace.

Error 1: 'module' object is not callable​

This error occurs when you treat the imported module object as if it were a function or a class constructor by putting parentheses () after its name.

Cause: Calling the Module Directly (module(...))​

Error Scenario 1: Built-in module​

main.py
# Error Scenario 1: Built-in module
import math

try:
# ⛔️ TypeError: 'module' object is not callable
# Trying to call the 'math' module itself.
result = math(3.14) # What function did you intend to call?
except TypeError as e:
print(e)

Error Scenario 2: Custom module​

another.py
def add(a, b):
return a + b
main.py
import another

try:
# ⛔️ TypeError: 'module' object is not callable
# Trying to call the 'another' module itself, not the 'add' function inside it.
sum_result = another(5, 10)
except TypeError as e:
print(f"\nCustom module error: {e}")

You cannot "call" the container (the module); you must call a specific function or class within it.

Solution 1: Access and Call Functions/Classes within the Module (module.function())​

Use dot notation (.) to access the desired function or class inside the module, and then call it with parentheses ().

another.py
def add(a, b):
return a + b
main.py
import math
import another

# ✅ Correct: Access function within the module, then call it
result_floor = math.floor(3.14) # Access 'floor' via dot, then call
print(f"math.floor(3.14) = {result_floor}") # Output: 3

result_add = another.add(5, 10) # Access 'add' via dot, then call
print(f"another.add(5, 10) = {result_add}") # Output: 15

Solution 2: Import Specific Functions/Classes Directly (from module import function)​

Import the specific function or class name directly into your current namespace. This allows you to call it without the module prefix.

another.py
def add(a, b):
return a + b
# ✅ Import specific names directly
from math import floor, ceil
from another import add

# ✅ Call the imported names directly
result_floor = floor(3.14) # No 'math.' prefix needed
result_ceil = ceil(3.14) # No 'math.' prefix needed
result_add = add(5, 10) # No 'another.' prefix needed

print(f"floor(3.14) = {result_floor}") # Output: floor(3.14) = 3
print(f"ceil(3.14) = {result_ceil}") # Output: ceil(3.14) = 4
print(f"add(5, 10) = {result_add}") # Output: add(5, 10) = 15

This is often preferred for commonly used functions as it makes the code less verbose.

Error 2: 'module' object is not subscriptable​

This error occurs when you use square brackets [...] with the imported module object, as if trying to access an item by index (like a list) or by key (like a dictionary).

Cause: Using Square Brackets on the Module (module[...])​

config.py
settings = {'host': 'localhost', 'port': 8080}
main.py
# Error Scenario: Custom module
import config

try:
# ⛔️ TypeError: 'module' object is not subscriptable
# Trying to access 'host' key directly from the module object
host_value = config['host']
except TypeError as e:
print(e)

Modules themselves don't support indexing or key lookup via [].

Solution 1: Access Variables/Collections within the Module First (module.variable[...])​

Use dot notation to access the specific variable (which might be a list, dictionary, etc.) inside the module, and then use square brackets on that variable.

config.py
settings = {'host': 'localhost', 'port': 8080}
main.py
import config

# ✅ Correct: Access the 'settings' dict via dot notation first...
settings_dict = config.settings
print(f"Settings dict: {settings_dict}")

# ✅ ...then use square brackets on the dictionary
host_value = settings_dict['host']
port_value = config.settings['port'] # Combine in one step

print(f"Host: {host_value}") # Output: Host: localhost
print(f"Port: {port_value}") # Output: Port: 8080

Solution 2: Import Specific Variables Directly (from module import variable)​

Import the specific variable (list, dictionary, etc.) directly into your current namespace.

config.py
settings = {'host': 'localhost', 'port': 8080}
main.py
# ✅ Import the dictionary variable directly
from config import settings

# ✅ Now access keys directly on the imported variable
host_value = settings['host']
port_value = settings['port']

print(f"Host: {host_value}") # Output: Host: localhost
print(f"Port: {port_value}") # Output: Port: 8080

Debugging with dir()​

If you're unsure what names (attributes, functions, classes, variables) are available within an imported module, use the dir() function.

config.py
settings = {'host': 'localhost', 'port': 8080}
import math

print("--- dir(math) ---")
print(dir(math))
# Output will include 'floor', 'ceil', 'pi', 'sqrt', etc. but NOT just 'math()'

print("\n--- dir(config) ---")
print(dir(config))
# Output will include '__name__', '__file__', etc. AND 'settings'
note
  • dir(module_object) lists the names accessible via dot notation from that module. It helps confirm the correct name to use (e.g., math.floor, config.settings).
  • It will NOT allow config['settings'] directly.

Conclusion​

TypeError exceptions involving 'module' object typically stem from treating the module itself as something it's not:

  • 'module' object is not callable: You tried to call the module like a function (module()). Solution: Call a specific function/class within the module using dot notation (module.function()) or import the function/class directly (from module import function, then function()).
  • 'module' object is not subscriptable: You tried to use square brackets on the module (module[...]). Solution: Access the desired variable (list, dict, etc.) within the module using dot notation first, then use square brackets on that variable (module.variable['key'] or module.variable[index]), or import the variable directly (from module import variable, then variable[...]).

Remember that import module_name gives you access to the module namespace, and you need to use dot notation or specific imports (from ... import ...) to access the items contained within that namespace.