"No module named 'pkg_resources'" means setuptools (which provides pkg_resources) is missing from your environment. Fix it with pip install --upgrade setuptools.
`pkg_resources` is not a standalone package you install on its own. It is a module that ships as part of **setuptools**. So `ModuleNotFoundError: No module named 'pkg_resources'` almost always means setuptools is missing from the Python environment that is running your code, or that it was removed/never installed. This has become much more common recently for two reasons: 1. Modern virtual environments no longer bundle setuptools. Since pip 22.1 / Python 3.12, `python -m venv` does not automatically install setuptools (only pip), so any code or dependency that does `import pkg_resources` fails until setuptools is added. 2. setuptools is in the process of removing `pkg_resources`. Starting with setuptools 81 (2025), importing `pkg_resources` emits a deprecation warning, and it is slated for full removal in a future release. On environments pinned to a very new setuptools, code that still relies on `pkg_resources` may break. In short: the component complaining is setuptools' runtime API. The fix is to install or restore setuptools (or, longer term, migrate code off the deprecated `pkg_resources` API).
pkg_resources lives in the environment that is actually running your code, so first confirm which Python that is:
which python3
python3 --version
python3 -c "import sys; print(sys.executable)"If you use a virtual environment, make sure it is activated (you should see its name in your prompt):
source myenv/bin/activate # Linux/macOS
myenv\Scripts\activate # WindowsBecause pkg_resources is provided by setuptools, installing/upgrading setuptools fixes the error in the vast majority of cases:
python3 -m pip install --upgrade setuptoolsUsing python3 -m pip (instead of a bare pip) guarantees the install goes into the same interpreter that raised the error. Then verify it imports:
python3 -c "import pkg_resources; print(pkg_resources.__file__)"If that prints a path with no error, the problem is resolved.
Virtual environments created with a recent python -m venv no longer install setuptools automatically. After creating and activating the venv, add setuptools (and refresh pip):
python3 -m venv myenv
source myenv/bin/activate # Windows: myenv\Scripts\activate
python -m pip install --upgrade pip setuptoolsThen reinstall your project dependencies inside the venv:
pip install -r requirements.txtsetuptools 81+ has begun deprecating and removing pkg_resources. If you control the project but cannot immediately migrate, declare setuptools as an explicit dependency and pin it to a version that still ships pkg_resources:
# requirements.txt
setuptools<81or in pyproject.toml:
[project]
dependencies = [
"setuptools<81",
]Treat this as a temporary bridge, not a permanent fix — see Advanced notes for migrating off pkg_resources.
If setuptools is present but the importing package is still broken, reinstall the offending package so its metadata is regenerated:
python3 -m pip install --force-reinstall <package-name>If the environment is in an inconsistent state (e.g. partial upgrades), the cleanest fix is to recreate it from scratch:
deactivate 2>/dev/null
rm -rf myenv # remove only the venv directory, not your project
python3 -m venv myenv
source myenv/bin/activate
python -m pip install --upgrade pip setuptools
pip install -r requirements.txtNote: rm -rf myenv deletes the virtual environment directory only — double-check the path so you don't remove project files. Recreating a venv is safe because it contains no source code, only installed dependencies.
Long-term, code should stop importing pkg_resources, since setuptools is removing it. Common replacements from the standard library:
- Reading installed package metadata/versions: use importlib.metadata (Python 3.8+). Replace pkg_resources.get_distribution("name").version with importlib.metadata.version("name").
- Accessing data files bundled in a package: use importlib.resources (Python 3.9+ for the modern files() API) instead of pkg_resources.resource_filename / resource_string.
- Parsing/comparing versions: use the packaging library (packaging.version.Version) instead of pkg_resources.parse_version.
System-managed Python (Debian/Ubuntu, Homebrew) may show this error because setuptools is provided by a separate OS package (e.g. python3-setuptools via apt) rather than pip. On those systems, prefer a virtual environment over pip install into the system interpreter to avoid the "externally-managed-environment" (PEP 668) restriction. Avoid pip install --break-system-packages unless you fully understand the consequences — it can corrupt the OS-managed Python; a venv is the safe alternative.
ValueError: math domain error
How to fix "ValueError: math domain error" in Python
ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/usr/lib/python3/dist-packages'
How to fix "Permission denied" when installing packages with pip
ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/usr/lib/python3/dist-packages'
How to fix "Read-only file system" error when installing packages with pip
socket.gaierror: [Errno -2] Name or service not known
How to fix "socket.gaierror: [Errno -2] Name or service not kn" in Python
pydantic_core._pydantic_core.ValidationError: 1 validation error
How to fix "pydantic_core._pydantic_core.ValidationError: 1 va" in Python