You run a Python script or install a package and the terminal stops with ModuleNotFoundError: no module named ‘pkg_resources’. The code worked last week, and nothing in your project changed. The cause is almost always the same: a recent setuptools release removed the module, or setuptools is missing from your environment altogether.
This guide explains why the error appears, how to fix it in minutes with a version pin, how to handle virtual environments, Docker and CI builds, and how to replace pkg_resources in your own code so the problem never returns.
What the error means
pkg_resources was an old helper module that shipped inside the setuptools package. Developers used it to read package versions, load data files and find plugins. The error means Python cannot find that module in the environment that is running your code.
Setuptools has warned about this for years. The module was deprecated, and import warnings appeared for almost three years before removal. Setuptools 82.0.0 then removed pkg_resources completely. Any code that still imports it now fails the moment a fresh environment installs the newest setuptools.
ModuleNotFoundError: no module named ‘pkg_resources’: common causes
Four situations account for nearly every report:
- You have setuptools 82 or newer. A new virtual environment, Docker image or CI runner pulled the latest release, which no longer contains the module.
- Setuptools is not installed. Python 3.12 and newer do not add setuptools to new virtual environments by default, so code that quietly relied on it breaks.
- An old package imports pkg_resources. A library you depend on has not migrated yet, and the error appears inside its files, not yours.
- A build fails during installation. Pip builds an older package from source and downloads the newest setuptools in an isolated build environment, where the module is missing.
Read the traceback from the bottom up. If the last frames point to a file in your own project, fix your code. If they point to site-packages, the culprit is a dependency.
Quick fix: pin setuptools below version 82
The fastest repair is to install a setuptools release that still includes pkg_resources. Run this in the same environment that shows the error:
python -m pip install "setuptools<82"
Then confirm the module imports:
python -c "import pkg_resources; print('ok')"
If you see ok, you are done for now. To check which version you have, run python -m pip show setuptools. Anything below 82 still ships the module.
Plain pip install setuptools used to be the standard answer, and many old forum posts still repeat it. That advice no longer works, because it installs the newest release, which has no pkg_resources. The version limit is the part that matters.
Fix it in virtual environments, Docker and CI
A pin typed into one terminal disappears the next time the environment is rebuilt. Make it permanent by adding it to the files that create your environment.
requirements.txt or constraints file
Add this line to your requirements file so every install uses a compatible release:
setuptools<82
Dockerfile
Install the pinned version before your other dependencies:
RUN pip install --no-cache-dir "setuptools<82" \
&& pip install --no-cache-dir -r requirements.txt
Conda
Conda users can pin the same limit with conda install "setuptools<82" inside the active environment.
CI pipelines
Add the pin step before tests run, or cache an environment that already contains a compatible setuptools. This is the most common reason a build that passed yesterday fails today with no code change.
Fix pip errors that happen while building a package
Sometimes the failure is not in your code. It happens while pip builds an older package from a source archive, and the traceback mentions a setup.py file. Pip creates an isolated build environment and downloads the latest setuptools there, so your own pin is ignored.
You have three practical options:
- Use build constraints. With pip 25.3 or newer, create a file named
build-constraints.txtthat containssetuptools<82, then runpip install --build-constraint build-constraints.txt package-name. You can also set thePIP_BUILD_CONSTRAINTenvironment variable. - Turn off build isolation. Install a compatible setuptools in your environment first, then run
pip install --no-build-isolation package-name. - Use a newer release. Try
pip install --upgrade package-name. Maintainers have been moving projects fromsetup.pytopyproject.toml, and a new version often removes the need forpkg_resources.
The uv and Poetry tools offer the same idea through their own settings, such as extra build dependencies in uv and a build constraints table in Poetry 2.3 and newer. The setuptools maintainers and users are still debating the change, and you can follow the discussion about restoring pkg_resources to see the workarounds people share.
Replace pkg_resources in your own code
If the import is in code you control, the lasting fix is to stop using the module. The standard library covers nearly everything it did, and these tools have been available since Python 3.9.
| Old pkg_resources code | Modern replacement |
|---|---|
pkg_resources.get_distribution("name").version |
importlib.metadata.version("name") |
pkg_resources.resource_filename("pkg", "file") |
importlib.resources.files("pkg").joinpath("file") |
pkg_resources.iter_entry_points("group") |
importlib.metadata.entry_points(group="group") |
pkg_resources.parse_version("1.2") |
packaging.version.parse("1.2") |
pkg_resources.declare_namespace(__name__) |
Native namespace packages, with no declaration |
A typical before and after looks like this:
# Before
import pkg_resources
version = pkg_resources.get_distribution("requests").version
# After
from importlib.metadata import version
version = version("requests")
The packaging library is a separate install, so add it to your dependencies if you use it. Namespace packages are the hardest case, because they cannot be swapped line for line. Most projects fix them by switching to native namespace packages, which need no special code at all.
Once your code no longer imports pkg_resources, you can drop setuptools from your runtime dependencies. If you are building developer skills along the way, our guide to how to become a full stack developer lists the tools worth learning, and our explainer on what instructors can actually view in Canvas is another quick technology read.
What not to do
- Do not run pip install pkg_resources. The module ships inside setuptools, and a package with that name will not restore it.
- Do not copy the module into your project. It is old code with known performance problems and no future updates.
- Do not ignore the version. Installing setuptools without a limit repeats the same error.
- Do not pin forever. A pin is a bridge. Plan to migrate or upgrade the package that needs the module.
Troubleshooting checklist
- Confirm the active interpreter with
python -m pip --version. Many people fix one environment and run code in another. - Run
python -m pip show setuptools. If it is missing or 82 or higher, install"setuptools<82". - Read the traceback to find whether your code or a dependency imports the module.
- Upgrade the dependency, or replace it if it is no longer maintained.
- Add the pin to requirements, your Dockerfile or CI so the fix survives a rebuild.
- Plan the migration to
importlib.metadataandimportlib.resources.
Frequently asked questions
Why did pkg_resources suddenly disappear?
Setuptools 82.0.0 removed the module after years of deprecation warnings. Environments that install the newest setuptools lose it automatically, which is why a project can break with no code change. Older setuptools versions still include it, which is why a version limit works as a quick fix.
Does pip install setuptools fix the error?
Not anymore. Plain installs now fetch the newest release, which no longer contains the module. Use pip install "setuptools<82" instead. The version limit is the part that restores pkg_resources, so keep it in your requirements file or Dockerfile.
Why does the error appear only inside Docker or CI?
Those systems build fresh environments and pull the latest setuptools every time. Your laptop may still have an older copy that works. Add a pinned setuptools line to the image or pipeline, or migrate the code, so every environment behaves the same way.
Why does pip fail while installing an old package?
Pip builds source archives in an isolated environment that downloads the latest setuptools, and an older setup.py may import pkg_resources. Use a build constraint file, disable build isolation after installing a compatible setuptools, or upgrade to a package release that supports modern packaging.
What replaces pkg_resources in 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 have been available since Python 3.9, so most projects can migrate without adding heavy dependencies.
Is it safe to pin setuptools below 82?
Yes, as a temporary measure. The pin keeps older code running while you migrate. Treat it as short term, because pinned tooling gets no future improvements. Plan to update your code or dependencies and remove the pin once nothing imports pkg_resources.
Conclusion
The error is not a sign of a broken Python install. A recent setuptools release removed a module that older code still imports. Pin setuptools below 82 to get moving, add the pin to your requirements, Dockerfile and CI, then migrate to importlib so your project stops depending on removed code.
David Weber is an experienced writer specializing in a range of topics, delivering insightful and informative content for diverse audiences.