Python Errors

Python TypeError: 'module' Object Is Not Callable — Causes & Fix

Python TypeError: module object is not callable — import datetime fix

What Does This Error Mean?

Python raises TypeError: 'module' object is not callable when you try to call a module as if it were a function or class. A module is a file — you cannot invoke it with (). The interpreter expects something callable (a function, a class) but receives a module object instead.

Common Causes (With Code)

1. Importing the module instead of the class

The most frequent cause happens with modules whose name matches the class inside them (datetime, socket, threading…). You import the module, then accidentally call the module itself.

❌ Causes the error

import datetime

# `datetime` here is the MODULE, not the class
fecha = datetime(2026, 3, 8)
# TypeError: 'module' object is not callable

❌ Same mistake with socket

import socket

s = socket()        # TypeError: 'module' object is not callable
# `socket` is the module — socket.socket is the class

2. Shadowing a module with a variable of the same name

If you use a module name as a variable name, all references to that module are lost after the assignment.

❌ Causes the error

import os

os = os.path.join("folder", "file.txt")  # `os` is now a string!
print(os.path.exists("test"))            # AttributeError / TypeError

3. A local file named the same as a standard library module

If your project has a file called datetime.py, Python will import it instead of the stdlib one. Your local file won't have the datetime class, causing the error downstream.

❌ Project structure that causes the error

my_project/
├── datetime.py       ← shadows the stdlib!
└── main.py

# main.py
from datetime import datetime   # imports YOUR datetime.py
now = datetime()                # TypeError — your file has no datetime class

How to Fix It

Fix 1 — Import the class directly

✅ Correct

from datetime import datetime

fecha = datetime(2026, 3, 8)
print(fecha)  # 2026-03-08 00:00:00

Fix 2 — Use dot notation to reach the class

✅ Correct

import datetime

fecha = datetime.datetime(2026, 3, 8)   # module.ClassName(...)
print(fecha)  # 2026-03-08 00:00:00

Fix 3 — For socket

✅ Correct

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("example.com", 80))

Fix 4 — Never use a module name as a variable name

✅ Correct

import os

path = os.path.join("folder", "file.txt")   # `os` stays untouched
print(os.path.exists(path))

Fix 5 — Rename local files that shadow the stdlib

Rename any local file that shares a name with a Python built-in module, then clear its compiled cache:

✅ Terminal

mv datetime.py my_datetime.py
find . -name "datetime.cpython*.pyc" -delete

Frequently Asked Questions

Why does this happen with datetime but not with math?

Because math.sin() and math.sqrt() are plain functions — you call them directly. The error appears when the module and class share the same name (datetime.datetime, socket.socket, threading.Thread), tempting you to skip the second level.

How do I quickly check whether something is a module or a callable?

Use type() or callable() directly in the REPL:

import datetime

print(type(datetime))           # <class 'module'>
print(callable(datetime))       # False  ← that's the problem

from datetime import datetime as dt
print(type(dt))                 # <class 'type'>
print(callable(dt))             # True   ← this is instantiable