Resolving "No module named 'setuptools.command.test'" during pip install
Problem Statement
When attempting to install packages like RandomWords
using pip (with Python 3.12+), you may encounter this error during installation:
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
python setup.py egg_info did not run successfully.
exit code: 1
[truncated]
from setuptools.command.test import test as TestCommand
ModuleNotFoundError: No module named 'setuptools.command.test'
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
Root Cause
This error occurs because:
- The package you're installing uses
setuptools.command.test
in its setup script. setuptools
version 72.0.0 (released July 28, 2024) removed this deprecated module.- Most Python environments automatically installed setuptools 72.0.0 when it was available.
Solutions
1. Update pip and reinstall (recommended)
bash
<tip> **Why this works:** Setuptools 72.0.0 has been withdrawn ("yanked") from PyPI. Upgrading pip ensures you get setuptools 71.x automatically. </tip> pip install --upgrade pip
pip install RandomWords
2. Constraint method (persistent projects/pipelines)
Create a constraints.txt
file at project root:
ini
setuptools<72
Then configure pip to use it:
Linux/macOS:
bash
echo "setuptools<72" > constraints.txt
export PIP_CONSTRAINT=constraints.txt
Windows (PowerShell):
powershell
"setuptools<72" | Out-File constraints.txt
$env:PIP_CONSTRAINT = "constraints.txt"
Dockerfile:
Dockerfile
RUN echo "setuptools<72" > constraints.txt
ENV PIP_CONSTRAINT=constraints.txt
3. Manual downgrade (immediate fix)
bash
pip install "setuptools<72"
4. Using --no-build-isolation
bash
pip install --no-build-isolation "setuptools<72"
pip install RandomWords
5. Poetry-specific solution
bash
<warning> **For legacy installations:** If poetry ignores constraints, use this workaround: ```bash poetry export -o requirements.txt poetry export -o constraints.txt -f constraints.txt PIP_CONSTRAINT=constraints.txt poetry run pip install -r requirements.txt ``` </warning> poetry add setuptools@<72
poetry install
Deep Dive: What Happened
- July 28, 2024: Setuptools 72.0.0 released with breaking changes (
setuptools.command.test
removed) - July 29-30: Widespread installation failures across Python ecosystem
- Key events:
- Setuptools 72.0.0 yanked from PyPI (ref)
- Removal of
command.test
postponed to November 2024 - Maintainers advised to migrate away from deprecated APIs
Best Practices for Prevention
Pin setuptools in critical projects:
python# pyproject.toml (Poetry example) [tool.poetry.dependencies] setuptools = "<72"
Use constraints in CI/CD pipelines
yaml# GitHub Actions example - name: Install dependencies env: PIP_CONSTRAINT: constraints.txt run: pip install -r requirements.txt
Verify dependencies:
bashpip list | grep setuptools # Should show version <=71.x