TypeError: 'module' object is not callable
1 · The lesson
readWhat this error means
You put () after the name of a module, as if it were a function. A module is the whole file you imported, like datetime or random. You can't call a file. You can only call the functions and classes inside it. This mix-up almost always happens because the module and the thing inside it have the same name: the datetime module contains a datetime class, and the random module contains a random() function.
When you see it
Traceback (most recent call last): File "main.py", line 3, in <module> today = datetime(2026, 11, 25) TypeError: 'module' object is not callable. Did you mean: 'datetime.datetime(...)'?
Python 3.12 and newer add the Did you mean hint, and the hint is right. Python 3.11 and older stop after not callable:
Traceback (most recent call last): File "main.py", line 3, in <module> roll = random() TypeError: 'module' object is not callable
Why it happens
1. The module and the thing inside share a name. import datetime gives you the module. The class you wanted is datetime.datetime. The same trap is in several standard modules:
| You wrote | The module | What you meant |
|---|---|---|
import datetime | datetime | datetime.datetime(...) |
import random | random | random.random() |
import glob | glob | glob.glob("*.py") |
import pprint | pprint | pprint.pprint(data) |
import time | time | time.time() |
import copy | copy | copy.copy(obj) |
import datetime today = datetime(2026, 11, 25) # TypeError: the module, not the class
2. Example code used the other import style. A tutorial wrote from datetime import datetime at the top, and you copied only the line below it into a file that has import datetime. Each line is correct on its own. They just don't go together.
3. Your own file and function share a name. You wrote greet.py with a function greet() inside, and then:
import greet greet("Ada") # TypeError: 'module' object is not callable
import greet gives you the file. The function is greet.greet.
How to fix it
Call the thing inside the module with module.name(...):
import datetime today = datetime.datetime(2026, 11, 25) print(today) # 2026-11-25 00:00:00 print(datetime.date(2026, 11, 25)) # 2026-11-25
Or import the name itself with from ... import .... Then the short name is the class or function:
from datetime import datetime from random import random, seed print(datetime(2026, 11, 25)) # 2026-11-25 00:00:00 seed(1) print(round(random(), 3)) # 0.134
Pick one style for each module and use it everywhere in the file. If a file has both import datetime and from datetime import datetime, whichever line runs last wins, and half your code breaks:
from datetime import datetime import datetime # this replaces the class with the module print(type(datetime)) # <class 'module'>
For your own files, either write greet.greet("Ada") or from greet import greet. Renaming the function to something different from the file name, like say_hello, also avoids the trap.
When you're not sure what a name is, print it. A module prints as <module ...>:
import random print(random) # <module 'random' from '...'> print(random.random) # <built-in method random of Random object at 0x...>
When you'd see this in real code
- The
tqdmprogress bar:import tqdmand thenfor x in tqdm(items):. The function istqdm.tqdm, so writefrom tqdm import tqdm. - A script that worked until someone tidied the imports and changed
from datetime import datetimeintoimport datetime. - In a Jupyter notebook: one cell runs
from datetime import datetime, a later cell runsimport datetime, and now the earlier cells fail when you run them again. - A helper file named after its main function, like
utils/slugify.pywithdef slugify(), imported asfrom utils import slugify. That gives you the file, soslugify(title)fails.
Related errors
- TypeError: 'X' object is not callable: the same
()mistake on a value, usually a variable that hides a built-in likelistorstr. - AttributeError: module 'X' has no attribute 'Y': you reached into the module, but the name isn't there. Often a file of yours named
random.pyorjson.pyhides the real module. - ModuleNotFoundError: No module named 'X': the import itself failed.
- ImportError: cannot import name 'X': the
from ... import ...style failed because the name isn't in that module.
See Also
- All Python errors: the full index, by type and by when it happens.
- Modules and imports (tutorial)
- Dates and times (tutorial)
- datetime cheat sheet
- Functions (tutorial)
Found this useful? Share it with a friend who’s learning Python.