PythonMastery
reference 3 min read · lesson 7 of 80 in Errors

TypeError: 'module' object is not callable

1 · The lesson

read

What 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

python
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:

python
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 wroteThe moduleWhat you meant
import datetimedatetimedatetime.datetime(...)
import randomrandomrandom.random()
import globglobglob.glob("*.py")
import pprintpprintpprint.pprint(data)
import timetimetime.time()
import copycopycopy.copy(obj)
python
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:

python
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(...):

python
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:

python
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:

python
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 ...>:

python
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 tqdm progress bar: import tqdm and then for x in tqdm(items):. The function is tqdm.tqdm, so write from tqdm import tqdm.
  • A script that worked until someone tidied the imports and changed from datetime import datetime into import datetime.
  • In a Jupyter notebook: one cell runs from datetime import datetime, a later cell runs import datetime, and now the earlier cells fail when you run them again.
  • A helper file named after its main function, like utils/slugify.py with def slugify(), imported as from utils import slugify. That gives you the file, so slugify(title) fails.

See Also

Found this useful? Share it with a friend who’s learning Python.