ModuleNotFoundError: No Module Named ‘pkg_resources’ Fix

You run a script or install a package and Python stops with ModuleNotFoundError: no module named ‘pkg_resources’. The code worked yesterday, nothing in your project changed, and now a missing module is blocking everything. The cause is almost always the same: a newer version of setuptools no longer provides it.

This guide explains what the error means, why it suddenly appears in some environments, and how to fix it in three ways: a quick pin, a build fix and a permanent code migration. Pick the option that matches your situation and you can be running again within minutes.

What ModuleNotFoundError: no module named ‘pkg_resources’ means

The module pkg_resources is not part of the Python standard library. It has always shipped inside the setuptools package, which is why code could import it without anyone installing it separately. Many older projects and build scripts used it to read package versions, find data files or load plugins.

When Python says the module cannot be found, one of two things has happened. Either setuptools is not installed in the environment you are using, or the installed setuptools is a recent release that has removed pkg_resources altogether. The setuptools maintainers have deprecated the module for years in favour of the standard library tools importlib.metadata and importlib.resources, and recent releases have finally dropped it.

Why the error appears now

Three common triggers explain why a working project suddenly breaks.

  • A setuptools upgrade. Running pip install --upgrade setuptools, or building a fresh environment that pulls the newest release, brings a version without pkg_resources. Reports point to versions 81 and 82 as the point where it disappeared for many users.
  • A newer Python version. Virtual environments created with Python 3.12 or later no longer install setuptools by default, so anything that quietly relied on it now fails.
  • Isolated builds. When pip builds a package from source it creates a temporary environment and installs a fresh setuptools. If that package’s setup.py still imports pkg_resources, the build fails even though your own environment looks fine.

Step 1: Check which environment is failing

Before changing anything, confirm that you are fixing the right Python. Many people install a package into one interpreter and run code with another.

  1. Activate the virtual environment you actually use for the project.
  2. Run python -m pip --version and check that the path points at that environment.
  3. Run python -m pip show setuptools to see whether setuptools is installed and which version you have.
  4. Read the full traceback. The file path on the line that imports pkg_resources tells you whether the problem is in your code, in a dependency or in a package’s setup.py during installation.

That last step matters. If the failing line sits inside your own project, the permanent fix is a code change. If it sits inside a third-party package, the best fix is usually to upgrade that package or pin setuptools until its authors release an update.

Step 2: The quick fix, pin setuptools to an older release

The fastest way to get moving is to install a version of setuptools that still includes pkg_resources. Use the same interpreter that shows the error:

python -m pip install "setuptools<81"

This restores the module in most cases. If you manage dependencies in a file, add the same pin there so the fix survives a rebuild:

setuptools<81

Treat this as a bridge rather than a destination. A pin keeps old code running, but it also means you are holding back a core packaging tool. Plan to remove it once the code that needs pkg_resources has been updated.

If setuptools is simply missing

On Python 3.12 and later you may see the error because setuptools was never installed. In that case a plain install is enough, though pinning to a release that still contains pkg_resources is the safer choice if your code imports it directly.

Step 3: Fix failing pip installs and builds

Sometimes the error appears while installing a package, with a long traceback ending in the missing module. That means the package’s build script imports pkg_resources inside pip’s temporary build environment, where the newest setuptools gets installed.

You have three practical options:

  1. Upgrade the package. Newer releases of many projects have already removed pkg_resources. Try python -m pip install --upgrade package-name first.
  2. Build without isolation. Install a compatible setuptools and the wheel package in your environment, then run python -m pip install --no-build-isolation package-name so pip uses your pinned version instead of fetching a new one.
  3. Choose a different version. If the latest release is the one that breaks, an earlier release of the package may install cleanly.

Docker and CI pipelines

Container images and CI runners create clean environments every time, which is why this error often shows up there first. Add the setuptools pin to your requirements file or install step, rebuild the image and confirm that the pin is applied before your application dependencies are installed.

Step 4: The permanent fix, remove pkg_resources from your code

If you own the code that imports pkg_resources, migrating is the cleanest solution. Modern Python includes everything you need, and the replacements are usually shorter than the old calls.

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) / "file"
pkg_resources.parse_version("1.2") packaging.version.Version("1.2")
pkg_resources.parse_requirements(text) packaging.requirements.Requirement(line)

For example, reading an installed package version becomes:

from importlib.metadata import version

print(version("requests"))

The packaging library is a separate install, so add it to your dependencies if you use it. You can read the official notes on the deprecation of the pkg_resources module to see which replacements are recommended for each use case.

Common mistakes to avoid

  • Installing setuptools into a different environment from the one that runs your code.
  • Upgrading setuptools to the newest version and expecting the error to disappear.
  • Pinning setuptools in your own environment but forgetting the isolated build step that has its own copy.
  • Editing files inside site-packages to remove the import. The change will be lost on the next install.
  • Leaving a pin in place for years without checking whether the dependency has been updated.

If you are working through other everyday technology problems, our guide on how to apply for a free tablet from the government covers a very different kind of setup, and the Technology section collects more practical guides. Beginners curious about the language itself can read why Python is worth learning for humanities students.

Frequently asked questions

Is pkg_resources part of the Python standard library?

No. It has always been distributed inside the setuptools package. That is why it can vanish when setuptools is missing or upgraded, even though your Python version has not changed. The standard library alternatives are importlib.metadata and importlib.resources.

Why does the error happen after upgrading setuptools?

Recent setuptools releases removed pkg_resources after years of deprecation warnings. Upgrading to the newest version replaces a release that still had the module with one that does not. Pinning an older release, or updating your code, brings your project back to a working state.

What is the quickest fix for ModuleNotFoundError: no module named ‘pkg_resources’?

Run python -m pip install "setuptools<81" in the environment that shows the error. This installs a release that still includes the module. It is a temporary measure, so plan to migrate to importlib.metadata or importlib.resources afterwards.

Why does the error appear only when installing a package?

Pip builds source packages in an isolated environment with a fresh setuptools. If the package’s setup script imports pkg_resources, the build fails there. Upgrade the package, use the no build isolation option with a pinned setuptools, or choose an earlier package version.

Should I replace pkg_resources in my own code?

Yes, if you control the code. The importlib.metadata and importlib.resources modules cover versions, entry points and data files, and packaging handles version parsing. Migrating removes the dependency on a deprecated module and stops the error returning after future upgrades.

Will the problem come back in a new virtual environment?

It can, because new environments often install the latest setuptools or none at all. Record the pin in your requirements file or lock file, and rebuild the environment from it. That way every machine and pipeline starts from the same known state.

Conclusion

ModuleNotFoundError: no module named ‘pkg_resources’ means your environment lacks a setuptools release that still ships the module. Confirm the right interpreter, pin setuptools below version 81 for a fast fix, and use no build isolation for stubborn installs. Then replace pkg_resources with importlib.metadata, importlib.resources and packaging so the error stays gone after future upgrades.

1 thought on “ModuleNotFoundError: No Module Named ‘pkg_resources’ Fix”

Leave a Comment