Skip to main content

How to Solve "No module named 'pkg_resources'" in Python

The error No module named 'pkg_resources' (or ImportError: No module named pkg_resources) in Python indicates that the setuptools package is either not installed or is outdated/corrupted. pkg_resources is a part of setuptools, and is used for finding and managing Python package/version dependencies.

This guide provides a step-by-step guide to resolve this error.

Understanding the Error

pkg_resources is a module within the setuptools package. setuptools is a fundamental package for building and distributing Python packages. Many other packages rely on pkg_resources to manage their dependencies. The error means the Python interpreter can't find this module, which almost always points to a problem with your setuptools installation.

Solution 1: Install or Upgrade setuptools (Most Common)

The first and most common solution is to install or upgrade setuptools using pip:

pip install --upgrade setuptools

pip install --upgrade setuptools: This command does two things:

  • Install: If setuptools is not installed, this will install it.
  • Upgrade: If setuptools is installed, this will upgrade it to the latest version. An outdated version can cause problems, so upgrading is a good first step.

If you have multiple Python versions or are using a specific pip version, use the appropriate command:

pip3 install --upgrade setuptools  # For Python 3 (may be pip3.x)

python -m pip install --upgrade setuptools # Use with specific Python executable
python3 -m pip install --upgrade setuptools #Use with specific Python executable
py -m pip install --upgrade setuptools #On Windows.
  • Using python -m pip, or the more specific python3 -m pip ensures that pip is run with the specific Python interpreter, to avoid problems when multiple Python interpreters are installed.

Solution 2: Reinstall setuptools

If upgrading doesn't work, try completely reinstalling setuptools. This can fix issues caused by corrupted installations:

pip uninstall setuptools
pip install setuptools

# Or, using the more specific commands:
python3 -m pip uninstall setuptools
python3 -m pip install setuptools

py -m pip uninstall setuptools # On Windows
py -m pip install setuptools # On Windows

  • First the setuptools is removed, then it is installed again.

Solution 3: Virtual Environments (Best Practice)

It's highly recommended to use virtual environments for Python development. This isolates your project's dependencies and prevents conflicts between different projects. If you're not already using virtual environments, this is a good time to start. If you are using a virtual environment, and you're getting this error, it likely means you haven't installed setuptools within the environment.

# Create a virtual environment (using the built-in venv module)
python3 -m venv venv # Or: python -m venv venv or py -m venv venv

# Activate the environment:
# On macOS/Linux:
source venv/bin/activate

# On Windows (cmd.exe):
venv\Scripts\activate.bat

# On Windows (PowerShell):
venv\Scripts\Activate.ps1

# Now, install your dependencies (setuptools will be installed automatically by venv)
pip install -r requirements.txt # If you have a requirements.txt
# OR, install a package that *depends* on setuptools:
pip install <some_package_that_needs_setuptools>
  • python3 -m venv venv: This creates a virtual environment named venv in your current directory. You can name it anything you want (e.g., .venv, my_project_env).
  • source venv/bin/activate (macOS/Linux) or venv\Scripts\activate.bat (Windows cmd.exe) or venv\Scripts\Activate.ps1 (Windows PowerShell): This activates the virtual environment. You'll see (venv) (or whatever you named your environment) at the beginning of your command prompt. This means that python and pip now refer to the versions within the environment.
  • pip install -r requirements.txt: If you have a requirements.txt file, this installs all your project's dependencies into the active virtual environment.
  • pip install <some_package_that_needs_setuptools>: If you don't have a requirements.txt yet, installing any package that depends on setuptools (which is almost all of them!) will automatically install setuptools into the environment.

The key point is that venv automatically includes setuptools when you create the environment. So, by using a virtual environment, you usually avoid this problem entirely.

Solution 4: get-pip.py (Last Resort)

If none of the above solutions work, and you can not install setuptools with pip, you can use the get-pip.py script. This is a last resort, as it's generally better to use your system's package manager or pip if possible.

  1. Download get-pip.py: Go to https://bootstrap.pypa.io/get-pip.py and save the file (right-click, "Save As...").

  2. Run the script:

python get-pip.py  # Or python3 get-pip.py, or py get-pip.py

This script will install pip, setuptools, and wheel into your Python installation.