No module named pkg_resources
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
No module named pkg_resources usually means the active Python environment does not have a working setuptools installation. The pkg_resources module is shipped by setuptools, so when that package is missing, broken, or installed into a different interpreter than the one you are running, the import fails. The fix is usually simple once you verify which Python environment is actually executing the code.
What pkg_resources is
pkg_resources is part of setuptools. Older tools and many third-party packages use it to inspect installed distributions, entry points, and package metadata.
A failing example looks like this:
If setuptools is missing from the active environment, Python raises ModuleNotFoundError or ImportError.
Fix the environment by installing setuptools
The first repair step is to install or upgrade setuptools in the same interpreter that runs the failing code.
On systems with multiple Python versions, use the exact interpreter you care about:
Using python -m pip is important because it binds pip to a specific interpreter. Running a bare pip install can accidentally update a different environment.
Verify the interpreter and the package location
If the error persists, print the interpreter path and check where setuptools is installed.
Those two commands often reveal the real issue. For example, your terminal may be using one interpreter while your IDE, notebook, or deployment environment is using another.
Virtual environments are a particularly common source of confusion. If your shell activates one environment but the editor launches a different interpreter, setuptools may exist in one place and be absent in the other.
Recreate a broken virtual environment if needed
Sometimes the environment is partially corrupted and reinstalling one package is not enough. In that case, recreating the virtual environment is usually faster than chasing every missing dependency manually.
Then reinstall your project dependencies normally. This gives you a clean baseline instead of trying to repair a damaged environment piecemeal.
Know when pkg_resources should be avoided
Modern Python packaging increasingly prefers lighter APIs such as importlib.metadata for metadata lookup. That does not change the immediate fix for this error, but it is useful context if you are maintaining your own code.
For example, if you only need a package version, modern code can often use:
This avoids taking a runtime dependency on pkg_resources for simple metadata queries.
Common Pitfalls
The biggest mistake is installing setuptools with the wrong pip. If pip points at one interpreter and your program runs under another, the error remains even though the install command looked successful.
Another issue is assuming the global Python environment matters when the failing code actually runs inside a virtual environment, Docker container, notebook kernel, or build tool.
Developers also sometimes delete or mix site-packages manually, which can leave setuptools half-installed. In those cases, recreating the environment is cleaner than patching it.
Finally, do not assume every pkg_resources import should remain forever. If it is your code, consider whether importlib.metadata is a better fit for the job now.
Summary
- '
pkg_resourcesis provided bysetuptools.' - Install or upgrade
setuptoolsin the exact interpreter that runs the code. - Use
python -m pipto avoid targeting the wrong environment. - Recreate the virtual environment if the installation is broken or inconsistent.
- Prefer newer metadata APIs in your own code when
pkg_resourcesis unnecessary.

