How do you fix "ModuleNotFoundError: No module named 'pkg_resources'" in Python? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you fix "ModuleNotFoundError: No module named 'pkg_resources'" in Python?

message-square
0

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_resources disappears on Python 3.12+ and newer setuptools:
    • Starting in Python 3.12, python -m venv stopped pre-installing setuptools and wheel into newly created virtual environments (only pip is installed).
    • Additionally, setuptools deprecated pkg_resources in v67.5.0 (due to slow import-time disk scanning) and dropped pkg_resources in v81+ (2025/2026). If an older unmaintained third-party library fails with No module named 'pkg_resources' even after pip install setuptools, pin pip install "setuptools<81" (or upgrade that third-party library to a newer version that migrated to importlib.metadata).
  • Always run python -m pip instead of bare pip:
    • Running bare pip install setuptools frequently installs setuptools into 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 ...).
  • Docker & CI/CD builds (python:3.12-slim / python:3.13-slim / uv):
    • Minimal Docker images and uv venv environments omit setuptools by default to keep images lean. Add setuptools<81 explicitly to your requirements.txt or pyproject.toml dependencies if any dependency still imports pkg_resources at runtime.
  • Namespace packages (pkg_resources.declare_namespace):
    • Legacy pkg_resources-style namespace packages in old libraries can fail when setuptools is missing; modern Python 3.3+ supports implicit PEP 420 namespace packages natively simply by omitting __init__.py in the namespace directory.

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.