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
setuptoolsout of the box:- Starting in Python 3.12,
python -m venv .venvonly seedspip(and omitssetuptoolsandwheel) because modern PEP 517/518 packages declare build dependencies dynamically inpyproject.toml. Older packages or scripts that executesetup.pydirectly or callpip install --no-build-isolationfail immediately unlesssetuptoolsis installed explicitly.
- Starting in Python 3.12,
- Fixing
No module named 'setuptools'duringpip install <legacy-pkg>(--no-build-isolationvs. normal build):- If you run
pip install --no-build-isolation <pkg>,pipskips creating a temporary build environment and relies on your active environment already havingsetuptoolsandwheel. Either remove--no-build-isolationor runpython -m pip install --upgrade setuptools wheelfirst.
- If you run
- Debian / Ubuntu system Python (
python3-setuptoolsvs.venv):- On Ubuntu 23.04+ / Debian 12+ (PEP 668 externally-managed environments), running
pip install setuptoolsglobally outside a virtual environment fails witherror: externally-managed-environment. Either activate a project virtual environment (python3 -m venv .venv && source .venv/bin/activate) and runpython -m pip install setuptools, or install the OS package viasudo apt update && sudo apt install -y python3-setuptools.
- On Ubuntu 23.04+ / Debian 12+ (PEP 668 externally-managed environments), running
- Fixing
No module named 'pkg_resources'alongsidesetuptools:pkg_resourcesships insidesetuptools(though deprecated insetuptools67+ and scheduled for removal). Installingsetuptoolsrestorespkg_resourcesfor legacy code, while new code should migrate to the standard library’simportlib.metadata(from importlib.metadata import version, files).
- Make sure
pythonandpippoint to the same interpreter:- Always invoke
python -m pip install setuptools(orpy -3.12 -m pip install setuptoolson Windows) rather than barepip install setuptoolssosetuptoolsis installed into the exact interpreter throwing theImportError.
- Always invoke
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. |
You must log in or register to comment.
