TypeError: 'module' object is not callable - 10 Causes and Fixes
TypeError: 'module' object is not callable means you called a module as if it were a function or a class. Python imported the file just fine - you then put parentheses after the module name, and a module has no __call__.
Ninety percent of the time the shape is this: the module and the thing inside it share a name. import datetime gives you the module datetime, not the class datetime.datetime. Same trap with socket, random (less so), logging.handlers, multiprocessing, and almost every single-class utility module in the standard library.
This page lists the distinct ways you get there - naming collisions, from X import *, package __init__.py that re-exports nothing, circular imports resolving to a partially initialised module, shadowed local files - each with a reproduction you can paste into a file and a fix that differs by the lines that matter.
The traceback always points at the call site, never at the import line. That is why this one wastes time.
| Error | TypeError: 'module' object is not callable |
|---|---|
| Where it happens | Python - CPython 2.7 through 3.x, any framework; raised at runtime on the call expression, not at import time |
| What it means | You put () after a name that is bound to a module object, and modules are not callable. |
The Fast Fix
You almost certainly imported the module when you wanted the class or function inside it.
# Broken: `datetime` is the module
import datetime
now = datetime.now() # TypeError: 'module' object is not callable
# Fix A - reach through the module
import datetime
now = datetime.datetime.now()
# Fix B - import the name you actually call
from datetime import datetime
now = datetime.now()
If the name is not from the standard library, run print(type(thing), thing) right before the failing line. If it prints <class 'module'>, you have the module; dir(thing) tells you which attribute inside it you meant.
If the import looks correct, you may be shadowing a module with a local file of the same name - jump to the diagnostic checklist.
What Is Actually Causing It
Jump to your case
- 1. Module and class share a name (datetime, socket, csv)
- 2. Calling your own module instead of its function
- 3. Package name equals class name, __init__.py re-exports nothing
- 4. A local file shadows a stdlib or third-party module
- 5. from package import name returns the submodule, not the function
- 6. Submodule import rebinds a name you imported earlier
- 7. from module import * pulled in a submodule of the same name
- 8. Circular import leaves a partially initialised module bound
- 9. Module imported where the class was expected (socket, Thread-style APIs)
- 10. Decorator or callback registered with a module reference
1. Module and class share a name (datetime, socket, csv)
Reproduce it
import datetime
stamp = datetime(2026, 1, 1)
# TypeError: 'module' object is not callable
Why it happens — import datetime binds the name datetime to the module. The class you wanted lives inside it as datetime.datetime. Python resolves the bare name to the module and then tries to call it.
The fix
from datetime import datetime
stamp = datetime(2026, 1, 1)
Changed the import: from datetime import datetime binds the class, not the module. The alternative is to keep import datetime and write datetime.datetime(2026, 1, 1).
Confirm it worked — python -c "from datetime import datetime; print(type(datetime))" prints <class 'type'>. With import datetime it prints <class 'module'>.
2. Calling your own module instead of its function
Reproduce it
# utils.py
def utils(x):
return x * 2
# main.py
import utils
print(utils(21))
# TypeError: 'module' object is not callable
Why it happens — import utils binds the module. The function utils is an attribute of that module, so the bare name is the file, not the function.
The fix
# main.py
from utils import utils
print(utils(21))
Or keep the module import and call through it: utils.utils(21). The import line changed; the call site is now unambiguous.
Confirm it worked — Add print(utils) above the call. A module prints <module 'utils' from '/path/utils.py'>; a function prints <function utils at 0x...>.
3. Package name equals class name, __init__.py re-exports nothing
Reproduce it
mypkg/
__init__.py # empty
mypkg.py # defines class Thing
import mypkg
obj = mypkg()
# TypeError: 'module' object is not callable
Why it happens — Importing a package gives you the package module object built from __init__.py. An empty __init__.py exports nothing callable, so the package name is just a namespace.
The fix
# mypkg/__init__.py
from .mypkg import Thing
__all__ = ["Thing"]
from mypkg import Thing
obj = Thing()
The __init__.py now re-exports the class, and the call site uses the class name instead of the package name.
Confirm it worked — python -c "import mypkg; print(mypkg.__all__)" lists Thing, and python -c "from mypkg import Thing; print(callable(Thing))" prints True.
4. A local file shadows a stdlib or third-party module
Reproduce it
# in your project root
touch random.py # empty file
import random
print(random.random())
# AttributeError, or with a submodule layout:
# TypeError: 'module' object is not callable
Why it happens — The script's own directory comes first on sys.path, so your empty random.py wins over the standard library. The name resolves to a module with none of the attributes you expect, and any call on the re-exported name fails.
The fix
mv random.py dice_utils.py
find . -name '__pycache__' -type d -exec rm -rf {} +
Rename the shadowing file and delete the stale .pyc cache - a leftover __pycache__/random.cpython-*.pyc keeps the shadow alive after the rename.
Confirm it worked — ```python import random print(random.file)
It must point inside your Python installation's `lib`, not your project directory.
---
<h3 id="cause-5">5. from package import name returns the submodule, not the function</h3>
**Reproduce it**
mypkg/ init.py loader.py # defines def loader(path): ...
```python
from mypkg import loader
loader("data.csv")
# TypeError: 'module' object is not callable
Why it happens — from pkg import name first looks for an attribute of the package; if __init__.py does not define loader, Python falls back to importing the submodule mypkg/loader.py and binds that module object.
The fix
from mypkg.loader import loader
loader("data.csv")
The import now descends one level further, so loader is the function inside the module of the same name.
Confirm it worked — print(loader.__module__) succeeds for a function and raises AttributeError for the module - or just check type(loader) is <class 'function'>.
6. Submodule import rebinds a name you imported earlier
Reproduce it
from os.path import join
import os.path as join # same name, now a module
print(join("a", "b"))
# TypeError: 'module' object is not callable
Why it happens — Imports are plain assignments executed top to bottom. The second line rebinds join to a module and silently discards the function you imported on line one.
The fix
from os.path import join
import os.path as path_mod
print(join("a", "b"))
The module alias changed to a non-colliding name, so join still refers to the function.
Confirm it worked — Search the file for the name: grep -n '\bjoin\b' yourfile.py. Each assignment or import of that name is a candidate rebind; only one should survive.
More causes (4 remaining)
7. from module import * pulled in a submodule of the same name
Reproduce it
mypkg/
__init__.py # contains: def parser(s): ...
parser.py
from mypkg import *
parser("x=1")
# TypeError: 'module' object is not callable
Why it happens — Without __all__, a star import exports every public attribute of the package - including submodules that got imported as a side effect. The submodule mypkg.parser overwrites the function mypkg.parser in your namespace depending on import order.
The fix
from mypkg import parser as parse_fn # explicit, no star
parse_fn("x=1")
Or define __all__ = ["parser"] in __init__.py and keep the star import. Either way the ambiguity is resolved explicitly rather than by import order.
Confirm it worked — After the import, print(type(parse_fn)) is <class 'function'>. In a REPL, from mypkg import *; print([n for n, v in vars().items() if type(v).__name__ == 'module']) lists every module a star import dragged in.
8. Circular import leaves a partially initialised module bound
Reproduce it
# a.py
import b
def make():
return b.build()
# b.py
import a
build = a # a is still half-initialised here
# main.py
import a
print(a.make())
# TypeError: 'module' object is not callable
Why it happens — During a cycle, Python puts a half-built module object in sys.modules so the second import succeeds. Any name bound from it at module level captures the module (or a missing attribute) rather than the finished function.
The fix
# b.py
def build():
import a # deferred to call time
return a.helper()
The import moved inside the function, so it runs after both modules are fully initialised and resolves to the real attribute.
Confirm it worked — Run with python -X importtime main.py to see the import order, or add print(sys.modules.get('a')) at the top of b.py - a partially initialised module shows up with its attributes missing.
9. Module imported where the class was expected (socket, Thread-style APIs)
Reproduce it
import socket
import multiprocessing
s = socket(socket.AF_INET, socket.SOCK_STREAM)
# TypeError: 'module' object is not callable
p = multiprocessing(target=print)
# TypeError: 'module' object is not callable
Why it happens — socket is a module whose constructor is socket.socket; multiprocessing is a package whose class is multiprocessing.Process. The lowercase module name reads like a constructor, which is why the mistake is so easy.
The fix
import socket
from multiprocessing import Process
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
p = Process(target=print, args=("hi",))
The socket call now goes through the module to the class of the same name; Process is imported by its real name.
Confirm it worked — python -c "import socket; print(callable(socket), callable(socket.socket))" prints False True. Apply the same two-part check to any module you are about to call.
10. Decorator or callback registered with a module reference
Reproduce it
import handlers # handlers.py defines def handle(evt): ...
callbacks = [handlers]
for cb in callbacks:
cb({"type": "click"})
# TypeError: 'module' object is not callable
Why it happens — The module went into the callback list instead of the function inside it. Nothing fails at registration time - the error surfaces much later at dispatch, far from the line that is actually wrong.
The fix
from handlers import handle
callbacks = [handle]
for cb in callbacks:
cb({"type": "click"})
The registration now stores the function. Guarding the registry with assert callable(cb) moves any future mistake back to the registration line.
Confirm it worked — At registration, assert all(callable(cb) for cb in callbacks). It fails immediately on the bad entry instead of at dispatch.
## None of Those? Narrow It Down
- Read the traceback's last frame and copy the exact name before the
(. The error is always about that one name - not about the function you are inside. If it is a dotted name likepkg.mod(...), the module is the final segment. - Print the object immediately above the failing line:
print(type(X), X).<class 'module'>plus a path confirms the diagnosis and the path tells you which file you actually got. Anything else means you are chasing the wrong error. - If the printed path is inside your project but you expected the standard library or a package, you have a shadowing file (cause 4). Rename it and clear
__pycache__. - Run
print(dir(X))to see what the module exports. A same-named entry -X.X- means module/class collision; a different name tells you exactly what to import instead. - Grep the file for every binding of that name:
grep -n 'import.*\bX\b\|^X *=' yourfile.py. More than one hit means a later line rebound the name to a module (causes 6 and 7). - If there is exactly one import and it looks correct, check the package's
__init__.py. An empty or non-re-exporting__init__.pymeans the package name resolves to a namespace with nothing callable (cause 3). - If the name is correct everywhere but fails only on some code paths, suspect a cycle: run
python -X importtime -c 'import yourmodule'and look for a module appearing before its dependency finished. That confirms cause 8 - move the import into the function. - Still unmatched: bisect by replacing the call with
Xalone and printing it at each layer up the stack. The first frame whereXis a module is where the wrong binding entered.
Why This Error Exists At All
In Python, everything is an object - modules included. A module is an instance of types.ModuleType with a __dict__ holding the names defined in its file. Calling an object means invoking its type's __call__ slot, and ModuleType does not define one. So datetime(2026, 1, 1) is not a special "import mistake" the interpreter detects - it is the generic failure of calling a non-callable, reported with the type's name filled in. That is why the message is so unhelpfully generic: the runtime has no idea you meant the class inside.
The second half of the design is that import X is an assignment. It binds one name in the current namespace, and the thing bound is the module object. from X import Y binds Y instead. Python keeps these two deliberately distinct because modules are first-class values you may legitimately want to pass around, inspect, or reload. The cost of that uniformity is that import datetime and from datetime import datetime look nearly identical but bind entirely different objects to the same identifier. The naming convention of the early standard library - module and its single class sharing a lowercase name - turns that distinction into a trap for datetime, socket, csv, pickle, and their relatives.
Once you see it as "a name is bound to the wrong kind of object", the whole family becomes recognisable. 'list' object is not callable is the same mistake with a variable that shadowed list. 'NoneType' object is not callable is a function that returned None where a callable was expected. 'dict' object is not callable is d(key) instead of d[key]. In every case the question is identical and answerable in one line: what is this name actually bound to right now? print(type(name)) answers it, and the answer is the fix.
Stop It From Coming Back
- Import the callable, not its container: prefer
from datetime import datetimeandfrom multiprocessing import Processoverimport datetimefollowed by a dotted call. One line of indirection removed is one class of bug removed. - Never name a file after a module you import. Add a CI check or pre-commit hook that lists your top-level
.pyfilenames and fails if any collides withsys.stdlib_module_names(available in Python 3.10+) or an installed distribution name. - Define
__all__in every package__init__.py. It stops star imports from leaking submodules into the caller's namespace and makes the package's public surface reviewable in one place. - Enable Pyflakes-based linting -
rufforflake8- which reports redefinition of an unused name (F811). That catches the "second import rebound the name" case (causes 6 and 7) before it ever runs. - Run mypy or Pyright in CI. A call on a module is a static type error (
Module not callable/not-callable), caught at check time instead of at the line that happens to execute in production. - Guard callback registries and plugin loaders at registration:
if not callable(fn): raise TypeError(f"{name} is {type(fn).__name__}, expected callable"). This moves the failure to the line that is wrong, not the line that dispatches.
Related Guides
Errors You Will Probably Hit Next
TypeError: 'NoneType' object is not callableAttributeError: module 'datetime' has no attribute 'now'ImportError: cannot import name 'X' from partially initialized module (most likely due to a circular import)
The traceback names the call site; the bug is in the import. When you see 'module' object is not callable, stop reading the function and print type() of the name in front of the parenthesis - module versus class answers it in one line. If the module's path points into your own project where you expected a library, you are shadowing it. Everything else is import X where you needed from X import Y.
댓글
댓글 쓰기