No module named pkg_resources
Categories:
Resolving 'No module named pkg_resources' in Python Environments

Encountering 'No module named pkg_resources' can halt your Python development. This guide explains why it happens, especially in virtual environments, and provides comprehensive solutions for Django, pip, and setuptools.
The error No module named pkg_resources
is a common frustration for Python developers, particularly when working with virtual environments, Django projects, or when managing packages with pip
and setuptools
. This module is a core component of setuptools
, which is essential for discovering and managing Python packages. Its absence typically indicates an issue with your Python environment's setup or package integrity.
Understanding pkg_resources and its Role
pkg_resources
is a module provided by the setuptools
package. It's a fundamental utility for runtime discovery of installed packages, their versions, and their entry points. Many Python tools and libraries, including pip
, virtualenv
, and various project dependencies, rely on pkg_resources
to function correctly. When this module is missing or corrupted, it breaks the chain of dependency resolution and package management, leading to the dreaded ImportError
.
flowchart TD A[Python Application/Script] --> B{Imports pkg_resources?} B -- Yes --> C[pkg_resources module] C --> D[setuptools package] D -- Provides --> C B -- No --> E[Continues Execution] C -- Missing/Corrupt --> F["ImportError: No module named pkg_resources"] F -- Caused by --> G[Incomplete setuptools installation] F -- Caused by --> H[Corrupted virtual environment] F -- Caused by --> I[Incorrect PATH/PYTHONPATH]
Flowchart illustrating the dependency on pkg_resources
and potential error causes.
Common Causes and Initial Troubleshooting
This error often arises from a few key scenarios:
- Incomplete
setuptools
Installation:pkg_resources
is part ofsetuptools
. Ifsetuptools
itself is not properly installed or has been partially removed, this error will occur. - Corrupted Virtual Environment: Virtual environments can sometimes become corrupted, leading to missing or broken symlinks for essential packages like
setuptools
. - Incorrect
PATH
orPYTHONPATH
: While less common in virtual environments, an improperly configuredPATH
orPYTHONPATH
could prevent Python from finding the installedsetuptools
. - Using
pip
from a different Python installation: If you have multiple Python versions, you might be using apip
associated with one Python installation to manage packages for another, leading to inconsistencies.
pip
commands or Python scripts to avoid conflicts between global and virtual environment packages.Solutions for 'No module named pkg_resources'
Here are the most effective solutions, starting with the simplest and most common fixes.
1. Reinstall setuptools and pip
This is often the quickest fix. If pkg_resources
is missing, reinstalling setuptools
and pip
usually resolves the issue by ensuring all necessary components are present and correctly linked. You might need to use python -m ensurepip
first if pip
itself is broken.
2. Upgrade setuptools and pip
Sometimes, an outdated version of setuptools
or pip
can cause issues. Upgrading them to the latest versions can resolve compatibility problems.
3. Recreate your Virtual Environment
If the virtual environment is corrupted, the most robust solution is to delete it and create a new one. This ensures a clean slate for your project dependencies.
4. Check Python Installation Integrity
In rare cases, the base Python installation itself might be damaged. Reinstalling Python (and then recreating virtual environments) can be a last resort.
Reinstall setuptools & pip
# If pip is completely broken, you might need to use ensurepip
python -m ensurepip --upgrade
# Then, upgrade setuptools and pip
python -m pip install --upgrade setuptools pip
Recreate Virtual Environment
# Deactivate current virtual environment if active
deactivate
# Navigate to your project directory
cd /path/to/your/project
# Remove the old virtual environment (e.g., 'venv' or '.venv')
rm -rf venv
# Create a new virtual environment
python3 -m venv venv
# Activate the new virtual environment
source venv/bin/activate
# Reinstall your project dependencies
pip install -r requirements.txt
requirements.txt
file (or similar dependency list) to easily reinstall all your project's packages. Otherwise, you'll have to install them manually.Specific Scenarios: Django and Global Installs
While the above solutions cover most cases, here are some considerations for specific contexts:
- Django Projects: If you encounter this error in a Django project, it almost always points to an issue with your virtual environment. Ensure it's activated and
setuptools
is correctly installed within it. - Global Python Installation: If you're not using a virtual environment (not recommended for development), the issue might be with your global Python installation. The solutions remain similar: reinstalling/upgrading
setuptools
andpip
for your global Python.
python -c "import pkg_resources; print(pkg_resources.__version__)"
Verify pkg_resources
installation and version after applying fixes.