ModuleNotFoundError: No Module Named ‘pkg_resources’ Fix

If your Python script suddenly fails with ModuleNotFoundError: no module named ‘pkg_resources’, you are not alone. The error shows up in fresh virtual environments, build logs and Docker images, and it usually appears on a machine that worked fine the day before.

The cause is almost always a newer setuptools release. The fix takes under a minute, but the right fix depends on whether you are running someone else’s code or building and maintaining your own.

Why You See ModuleNotFoundError: No Module Named ‘pkg_resources’

The pkg_resources module was never a separate package. It shipped inside setuptools, and for years it was the default way to read package versions, find entry points and load data files. Many old projects, build scripts and command line tools still import it on the first line.

What changed in setuptools

Setuptools version 82.0.0, released on 8 February 2026, removed pkg_resources entirely. The release notes say that most common uses have been replaced by importlib.resources and importlib.metadata, and that projects which still need pkg_resources should depend on an older setuptools. If your environment installs the newest setuptools, any import of pkg_resources now fails.

The removal was not a surprise. Setuptools began emitting a DeprecationWarning when pkg_resources was imported in version 67.5.0 in March 2023, and a May 2025 release set a formal deadline for its removal. Plenty of projects ignored those warnings, which is why the break feels sudden.

Why a clean install can also fail

There is a second cause. Newer Python versions, starting with 3.12, no longer install setuptools into new virtual environments by default. If a package imports pkg_resources and you never installed setuptools, Python cannot find the module at all. So the same error message can mean either “setuptools is missing” or “setuptools is too new”.

Check which one applies by running these two commands inside the environment that fails:

python --version
pip show setuptools

If pip reports that setuptools is not installed, you need to install it. If it reports version 82 or higher, you need an older version or an updated package.

Quick Fixes That Work

1. Install or pin setuptools below version 82

This is the fastest fix and it solves most cases. Setuptools 81 and earlier still include pkg_resources, so pinning below 82 brings the module back:

pip install "setuptools<82"
python -c "import pkg_resources; print('pkg_resources is available')"

If you use uv, the equivalent command is uv pip install "setuptools<82". Treat the pin as a short-term repair. It keeps the program running while you or the package maintainers move away from pkg_resources.

2. Recreate a broken virtual environment

Sometimes an environment has been upgraded in place and ends up with a mix of old and new packages. Rather than patching it, create a new one and install your requirements with the pin included:

python -m venv .venv
source .venv/bin/activate
pip install "setuptools<82" wheel
pip install -r requirements.txt

On Windows, activate the environment with .venv\Scripts\activate instead. This approach is also the usual answer for large tools with many dependencies, such as local image generation web interfaces, where a stale environment is the real problem.

3. Fix build failures during pip install

Sometimes the error appears while pip is building a package from source, with a traceback that mentions build_meta or setup.py. In that case the package’s setup file imports pkg_resources, and pip’s isolated build environment has just downloaded the latest setuptools. Installing a pinned setuptools into your own environment does not help unless you turn off build isolation:

pip install "setuptools<82" wheel
pip install --no-build-isolation package-name

Better still, check whether the package has a newer release. Maintainers have been replacing pkg_resources.parse_version with packaging.version.parse and adding packaging to their build requirements, so upgrading the package is often the clean solution.

4. Fix Docker and CI images

Container builds break because they always pull the newest tools. Add the pin before your dependencies are installed so the build is repeatable:

RUN pip install --no-cache-dir "setuptools<82" wheel
RUN pip install --no-cache-dir -r requirements.txt

Pin the same version in your CI configuration, otherwise tests may pass on a laptop and fail in the pipeline.

5. Fix tools installed with pipx

Command line tools installed with pipx live in their own environments. If one of them fails with this error, inject a compatible setuptools into that tool’s environment:

pipx inject tool-name "setuptools<82"

How to Fix Your Own Code for Good

If the failing import is in code you control, stop depending on pkg_resources. The replacements are part of the standard library or a small, widely used package, so nothing extra is needed in most cases.

Old pkg_resources call Modern replacement
pkg_resources.get_distribution(“name”).version importlib.metadata.version(“name”)
pkg_resources.iter_entry_points(“group”) importlib.metadata.entry_points(group=”group”)
pkg_resources.resource_filename(“pkg”, “file”) importlib.resources.files(“pkg”).joinpath(“file”)
pkg_resources.parse_version(“1.2.3”) packaging.version.parse(“1.2.3”)

Here is a typical before and after for reading a package version:

# Before
import pkg_resources
version = pkg_resources.get_distribution("requests").version

# After
from importlib.metadata import version
current = version("requests")

Namespace packages need a different approach. The setuptools team recommends implicit namespace packages as described in PEP 420, which means leaving out the __init__.py file in the namespace folder instead of calling declare_namespace. After the change, add packaging to your dependencies if you use parse_version, and run your tests with the newest setuptools to confirm nothing else relied on the old module.

The setuptools team keeps a clear record of what changed and when in the setuptools release history, which is worth checking when a dependency breaks after an upgrade.

Which Fix Should You Choose?

Use the situation to pick the right repair instead of trying everything at once:

  • You are just running a script or tool: pin setuptools below 82 in its environment.
  • A package fails while installing: upgrade the package first, then use no build isolation with a pinned setuptools if you must.
  • You maintain a library or app: replace pkg_resources with importlib.metadata, importlib.resources and packaging.
  • Your Docker or CI build broke: pin setuptools in the image and in the pipeline until dependencies catch up.

The same habit of checking the cause before applying a fix applies outside programming too. For another step-by-step walkthrough on this site, see our guide on how to apply for a free tablet from the government, and for a more hands-on planning guide, read how to excavate a construction site.

How to Stop the Error From Coming Back

A few small habits will keep this class of error from returning with the next setuptools change:

  1. Pin your build tools in requirements files, lock files or constraints so every machine installs the same versions.
  2. Read DeprecationWarning messages in your test output instead of hiding them. They usually name the replacement.
  3. Run one CI job against the newest setuptools and pip, so a future break shows up in a test rather than in production.
  4. Avoid installing packages globally. A separate virtual environment per project limits the damage when one tool needs an old version.

Frequently asked questions

What does no module named ‘pkg_resources’ mean?

It means Python cannot find pkg_resources, a module that used to ship inside setuptools. Either setuptools is not installed in the active environment, or the installed version is 82 or newer, which removed the module. Pinning setuptools below 82 usually fixes it.

Which setuptools version removed pkg_resources?

Setuptools 82.0.0, released on 8 February 2026, removed pkg_resources. Version 81 and earlier still include it, which is why installing a setuptools version below 82 restores the module. Newer 83 and 84 releases do not bring it back.

Is it safe to pin setuptools to an older version?

For most projects, yes. Pinning below 82 is a safe short-term repair for scripts, containers and build jobs. The downside is that you miss newer fixes, so plan to update the code that imports pkg_resources and remove the pin later.

Why does the error happen only when installing a package?

Pip builds many packages in an isolated environment that downloads the latest setuptools. If the package’s setup file imports pkg_resources, the build fails even though your own environment is fine. Upgrade the package, or install with no build isolation after pinning setuptools.

What replaces pkg_resources in modern Python?

The standard library covers most uses. Use importlib.metadata for versions and entry points, importlib.resources for data files, and the packaging library for version parsing. These tools work without setuptools and are the approach the setuptools team recommends for new code.

Why does a new virtual environment not have setuptools?

Python 3.12 and newer stopped installing setuptools into new virtual environments by default. Packages that still import pkg_resources will fail until you install it yourself. Add a pinned setuptools to your requirements file, or move the code to importlib so it is no longer needed.

Conclusion

The pkg_resources error is a side effect of setuptools 82 removing a module that old code still relied on. For a quick fix, install setuptools below version 82, recreate a broken environment or turn off build isolation. For a lasting fix, replace pkg_resources calls with importlib and packaging, then test against the newest setuptools so future changes stay harmless.

Leave a Comment