ModuleNotFoundError: No Module Named ‘pkg_resources’ Fix

If you see ModuleNotFoundError: no module named ‘pkg_resources’ when you run a script or install a package, Python is telling you that the pkg_resources module is missing from the environment you are using. That module used to arrive automatically with setuptools, but recent changes to Python and to setuptools mean it can no longer be taken for granted.

The fix is often a single command. This guide explains what causes the error, walks through the quickest fixes in the order they tend to work, and covers pip builds, Docker images and code changes for developers.

What the ModuleNotFoundError: no module named ‘pkg_resources’ Error Means

pkg_resources is a module that was distributed inside setuptools. Older packages import it to read package metadata, find entry points or locate data files that ship with a library. When Python cannot find the module in the active environment, it raises ModuleNotFoundError at the exact line that tries to import it. The error does not mean Python itself is broken. It means the environment is missing a piece that some code expects.

The traceback tells you where to look. If the lines above the error mention a setup.py or build_meta.py file, the failure is happening while pip builds a package. If they point to a script or a library inside site-packages, the failure is happening at runtime. Those two cases need slightly different fixes, so check this before you start typing commands.

Why the Error Is Showing Up Now

Several changes landed close together, which is why the same message appears in so many unrelated projects.

  • New virtual environments are leaner. Since Python 3.12, a fresh virtual environment no longer installs setuptools by default. Code that quietly relied on it stops working after an upgrade.
  • Newer setuptools releases dropped the module. Developers have reported that setuptools releases from around version 81 and 82 no longer include pkg_resources, so even a plain upgrade of setuptools may not bring it back.
  • Build isolation pulls the latest tools. When pip builds a package, it creates a temporary environment and installs the newest build requirements. If the package asks for setuptools without a version limit, it can get a release that has no pkg_resources.
  • Old libraries are still around. Many packages written years ago import pkg_resources and have not been updated.

Quick Fixes to Try First

Work through these steps in order. Most people are done by the second or third one.

Step 1: Confirm Which Python You Are Using

A common cause of confusion is installing a package into one Python while running code with another. Use the interpreter name in front of pip so both point to the same place:

python --version
python -m pip --version
python -m pip show setuptools
python -c "import pkg_resources"

If the last command prints nothing, the module is available and the problem lies elsewhere, such as a different interpreter inside your editor or a build tool. If it prints the same ModuleNotFoundError, continue to the next step.

Step 2: Install or Upgrade setuptools

Install setuptools into the active environment and test the import again:

python -m pip install --upgrade setuptools
python -c "import pkg_resources"

If the error disappears, you are finished. If it remains, the setuptools version you just installed probably no longer contains pkg_resources, which leads to the next step.

Step 3: Pin an Older setuptools

Pinning setuptools below the release that removed the module is the most widely used workaround. Reports from developers mention a pin such as 80.10.2:

python -m pip install "setuptools<81"

Treat this as a temporary measure. It keeps old code running while you or the package maintainers move to modern alternatives. Write the pin into your requirements file so every teammate and every server gets the same version, and add a reminder to remove it later.

Step 4: Recreate the Virtual Environment

If the environment has been upgraded and downgraded several times, it may be easier to start clean. Create a new one, activate it, and install your dependencies again:

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

On Windows, activate the environment with .venv\Scripts\activate instead. Installing the setuptools pin before your other requirements helps when a dependency needs pkg_resources during its own installation.

Fixing the Error While pip Installs a Package

If the traceback mentions a setup.py or build_meta.py file, the failure happens inside pip’s temporary build environment. Installing setuptools in your own environment does not help there, because pip builds in an isolated space and downloads its own build tools.

You have three practical options:

  1. Turn off build isolation for that install. First install a compatible setuptools and wheel in your environment, then run the install with the isolation flag turned off. You must install any other build dependencies the package needs yourself.
  2. Upgrade to a newer release of the package. Many maintainers have already fixed their build files, so the newest version may install without trouble.
  3. Fix the build requirements if you maintain the package. Limit setuptools in the build-system section, or better, remove the pkg_resources import from setup.py.
python -m pip install "setuptools<81" wheel
python -m pip install --no-build-isolation package-name

For a package you maintain, a pyproject.toml entry like the following keeps builds working while you migrate:

[build-system]
requires = ["setuptools<81", "wheel"]
build-backend = "setuptools.build_meta"

Fixing It in Docker and CI

Docker images built on recent Python versions often leave out setuptools, so a project that worked on your laptop can fail inside a container. Add the pin to the image before installing the rest of the requirements:

RUN pip install "setuptools<81"
COPY requirements.txt .
RUN pip install -r requirements.txt

In continuous integration, put the same pin in the requirements or constraints file instead of installing it by hand in a script. That way the build is repeatable, and a new setuptools release cannot break your pipeline overnight. If the container error appears only in some builds, check whether a cached layer is using an older or newer setuptools than the one you tested.

Update the Package That Still Imports pkg_resources

A pin gets you running, but the better long-term fix is to update the library that needs the module. The traceback names it. Upgrade that package first and read its release notes for mentions of pkg_resources or importlib.metadata.

For example, one Django project fixed the error by upgrading the pysolr library from version 3.9.0 to 3.11.0 or newer, because the older release depended on pkg_resources. You can list outdated packages in your environment with the command below, then upgrade the one in the traceback:

python -m pip list --outdated

If the package has not had a release in years, look for a maintained fork or an alternative library. Depending on an abandoned package that needs a removed module will cause the same trouble again later.

For Developers: Replace pkg_resources With importlib

If you wrote the code that imports pkg_resources, the cleanest solution is to stop using it. Python’s own documentation explains that importlib.metadata provides the entry point and metadata features that pkg_resources used to offer, and that together with importlib.resources it supersedes the old module. The setuptools project also keeps a documentation page for pkg_resources that describes its status.

These examples show the most common replacements.

Reading a package version

# Old
import pkg_resources
pkg_resources.get_distribution("requests").version

# New
from importlib.metadata import version
version("requests")

Finding entry points

from importlib.metadata import entry_points
scripts = entry_points(group="console_scripts")

Reading a data file that ships with your package

from importlib.resources import files
text = files("mypackage").joinpath("data.txt").read_text()

The metadata module has been part of Python since version 3.8, the group keyword for entry points needs Python 3.10 or later, and the files helper is available from Python 3.9. If you must support older versions, the importlib_metadata and importlib_resources backports offer the same interfaces.

Which Fix Fits Your Situation

Situation Likely cause Best fix
Script fails after creating a new virtual environment setuptools is not installed by default on newer Python Install a compatible setuptools in that environment
Error remains after upgrading setuptools The new release has no pkg_resources Pin setuptools below version 81
Traceback shows setup.py or build_meta.py Isolated build picked a new setuptools Install without build isolation or upgrade the package
Works locally but fails in Docker The image does not include setuptools Add the pin to the Dockerfile before other installs
You maintain the failing code Code imports a removed module Move to importlib.metadata and importlib.resources

Mistakes to Avoid

  • Using a bare pip command. It may belong to a different Python than the one running your code. Use python -m pip instead.
  • Installing into the system Python with administrator rights. This can damage tools your operating system relies on. Use a virtual environment.
  • Leaving the pin in place forever. A pin buys time. Plan to update or replace the package that needs it.
  • Skipping the traceback. The file names tell you whether the problem is a build, a runtime import or a container issue.

Small setup problems often look bigger than they are, much like the confusion around what Canvas actually records during a quiz, where the real answer turns out to be narrower than people fear. And if you are setting up a device for someone else, our guide on how to apply for a free tablet from the government explains the programs that can help with the cost.

Frequently Asked Questions

What does ModuleNotFoundError: no module named ‘pkg_resources’ mean?

It means Python tried to import pkg_resources and could not find it in the active environment. The module used to ship with setuptools, so the usual cause is a missing or newer setuptools, or a different interpreter than the one you installed packages into.

Is pkg_resources part of Python or setuptools?

It was part of setuptools, not the Python standard library. Python’s own replacements are importlib.metadata and importlib.resources, which cover reading metadata, entry points and package data without installing anything extra on current Python versions.

Why does pip install setuptools not fix the error?

Recent setuptools releases no longer include pkg_resources, so installing the latest version can leave the module missing. Pin an older release such as “setuptools<81” instead, or update the package that imports it so it no longer needs the module.

Which setuptools version should I use?

Developers who hit this problem commonly pin setuptools below version 81, with 80.10.2 mentioned as a working example. Use the newest version below that limit, and remove the pin once your dependencies have moved to importlib.

Is pkg_resources deprecated?

Yes. Python’s documentation describes it as removed from newer setuptools and superseded by importlib.metadata and importlib.resources. Existing code may still run on older setuptools, but new projects should avoid it and migrate old code when possible.

How do I fix the error in Docker?

Add a line that installs a compatible setuptools, such as pip install “setuptools<81”, before the step that installs your requirements. Then rebuild without cache so an old layer does not hide the change, and test the import inside the container.

How do I fix it when pip fails during installation?

Pip builds packages in an isolated environment, so your own setuptools is ignored. Install a compatible setuptools and wheel first, then run the install with build isolation disabled, or upgrade to a newer release of the package that fixes its build files.

Conclusion

The ModuleNotFoundError: no module named ‘pkg_resources’ error usually means your environment lacks a compatible setuptools or a package still depends on an old module. Start by confirming the interpreter, then install or pin setuptools, and handle build isolation when pip fails. After that, upgrade the package that imports pkg_resources, or move your own code to importlib.metadata so the problem does not return.

Leave a Comment