How do you fix "ImportError: No module named setuptools" in Python 3? | A1Lab@MZeeshanM to PythonEnglish • 3 days ago

How do you fix ""ImportError: No module named setuptools"" in Python 3?

message-square
0

Install or upgrade setuptools inside the exact Python interpreter or virtual environment running your command via python -m pip install --upgrade setuptools (or recreate your virtual environment if pip itself is missing using python -m ensurepip --upgrade), because Python 3.12+ no longer pre-installs setuptools or wheel inside newly created venv environments by default.

Basic Example

# 1. Recommended: Install/upgrade `setuptools` in the active Python environment
python -m pip install --upgrade setuptools

# 2. If `pip` itself is also missing (common in minimal Docker images or fresh Python 3.12+ venvs):
python -m ensurepip --upgrade
python -m pip install --upgrade pip setuptools wheel

# 3. Verify `setuptools` imports cleanly:
python -c "import setuptools; print(setuptools.__version__)"

Key Details & Edge Cases

  • Why Python 3.12+ virtual environments lack setuptools out of the box:
    • Starting in Python 3.12, python -m venv .venv only seeds pip (and omits setuptools and wheel) because modern PEP 517/518 packages declare build dependencies dynamically in pyproject.toml. Older packages or scripts that execute setup.py directly or call pip install --no-build-isolation fail immediately unless setuptools is installed explicitly.
  • Fixing No module named 'setuptools' during pip install <legacy-pkg> (--no-build-isolation vs. normal build):
    • If you run pip install --no-build-isolation <pkg>, pip skips creating a temporary build environment and relies on your active environment already having setuptools and wheel. Either remove --no-build-isolation or run python -m pip install --upgrade setuptools wheel first.
  • Debian / Ubuntu system Python (python3-setuptools vs. venv):
    • On Ubuntu 23.04+ / Debian 12+ (PEP 668 externally-managed environments), running pip install setuptools globally outside a virtual environment fails with error: externally-managed-environment. Either activate a project virtual environment (python3 -m venv .venv && source .venv/bin/activate) and run python -m pip install setuptools, or install the OS package via sudo apt update && sudo apt install -y python3-setuptools.
  • Fixing No module named 'pkg_resources' alongside setuptools:
    • pkg_resources ships inside setuptools (though deprecated in setuptools 67+ and scheduled for removal). Installing setuptools restores pkg_resources for legacy code, while new code should migrate to the standard library’s importlib.metadata (from importlib.metadata import version, files).
  • Make sure python and pip point to the same interpreter:
    • Always invoke python -m pip install setuptools (or py -3.12 -m pip install setuptools on Windows) rather than bare pip install setuptools so setuptools is installed into the exact interpreter throwing the ImportError.

Comparison of Fixes by Environment Scenario

Environment / Cause Exact Fix Command Preserves System Python Isolation? Best Used When
Active Virtual Environment (venv) on Python 3.12+ (Recommended) python -m pip install --upgrade setuptools wheel Yes Building legacy setup.py packages or running scripts that import setuptools / pkg_resources.
Broken / Minimal Environment Missing Both pip and setuptools python -m ensurepip --upgrade && python -m pip install --upgrade pip setuptools Yes (when run inside .venv) Slim Docker images (python:3.12-slim) or stripped-down Python installs.
Debian / Ubuntu System-Wide CLI Tools (PEP 668) sudo apt update && sudo apt install -y python3-setuptools Yes (managed cleanly by apt) System scripts on Linux that intentionally run against /usr/bin/python3 outside a venv.
Modern Package Development (pyproject.toml) Add requires = ["setuptools>=61.0"] under [build-system] in pyproject.toml Yes (PEP 517 isolated builds) Authoring new packages so pip and build auto-install setuptools during builds.