Install or upgrade setuptools inside your active Python environment (python -m pip install --upgrade setuptools), because pkg_resources is bundled inside setuptools and is no longer pre-installed by default in Python 3.12+ virtual environments (venv) or uv-created environments—and migrate your own code to importlib.metadata and importlib.resources since pkg_resources was deprecated in setuptools 67+ and removed in modern setuptools 80+ releases.
Basic Example
# 1. Immediate fix for third-party libraries that still import pkg_resources:
python -m pip install --upgrade "setuptools<81"
# 2. If `pip` itself is missing or broken in the virtual environment, bootstrap it first:
python -m ensurepip --upgrade
python -m pip install --upgrade "setuptools<81"
# 3. Recommended long-term replacement in YOUR own code (Python 3.9+ stdlib):
from importlib.metadata import version, PackageNotFoundError
from importlib.resources import files
# Replaces: pkg_resources.get_distribution("requests").version
try:
print(version("requests"))
except PackageNotFoundError:
print("requests is not installed")
# Replaces: pkg_resources.resource_string("my_pkg", "data/config.json")
config_text = (files("my_pkg") / "data/config.json").read_text(encoding="utf-8")
Key Details & Edge Cases
- Why
pkg_resourcesdisappears on Python 3.12+ and newersetuptools:- Starting in Python 3.12,
python -m venvstopped pre-installingsetuptoolsandwheelinto newly created virtual environments (onlypipis installed). - Additionally,
setuptoolsdeprecatedpkg_resourcesin v67.5.0 (due to slow import-time disk scanning) and droppedpkg_resourcesin v81+ (2025/2026). If an older unmaintained third-party library fails withNo module named 'pkg_resources'even afterpip install setuptools, pinpip install "setuptools<81"(or upgrade that third-party library to a newer version that migrated toimportlib.metadata).
- Starting in Python 3.12,
- Always run
python -m pipinstead of barepip:- Running bare
pip install setuptoolsfrequently installssetuptoolsinto a global or different Python installation while your script runs inside.venv. Always target the exact interpreter throwing the error (python -m pip install ...or.venv/bin/python -m pip install ...).
- Running bare
- Docker & CI/CD builds (
python:3.12-slim/python:3.13-slim/uv):- Minimal Docker images and
uv venvenvironments omitsetuptoolsby default to keep images lean. Addsetuptools<81explicitly to yourrequirements.txtorpyproject.tomldependencies if any dependency still importspkg_resourcesat runtime.
- Minimal Docker images and
- Namespace packages (
pkg_resources.declare_namespace):- Legacy
pkg_resources-style namespace packages in old libraries can fail whensetuptoolsis missing; modern Python 3.3+ supports implicit PEP 420 namespace packages natively simply by omitting__init__.pyin the namespace directory.
- Legacy
Comparison of Fixes & Replacements for pkg_resources
| Solution | Scope | Requires Changing Source Code? | Future-Proof for Python 3.13+ / setuptools 81+? |
Best Used When |
|---|---|---|---|---|
python -m pip install "setuptools<81" (Immediate Fix) |
Active .venv / environment |
No | Moderate (unblocks legacy third-party dependencies) | A third-party dependency fails on import pkg_resources in Python 3.12+ or uv. |
importlib.metadata.version() (Stdlib Replacement) |
Your codebase (Python 3.8+) | Yes | Yes (built-in standard library, ~10× faster startup) | Checking installed package versions or entry points (entry_points()) in your own code. |
importlib.resources.files() (Stdlib Replacement) |
Your codebase (Python 3.9+) | Yes | Yes (built-in standard library, zip-safe) | Reading bundled data files, templates, or schemas packaged inside a Python module. |
| Upgrade the offending third-party package | requirements.txt / pyproject.toml |
No | Yes | An actively maintained library has already released a patch removing its pkg_resources dependency. |
You must log in or register to comment.
