Skip to content

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:

  1. The package you're installing uses setuptools.command.test in its setup script.
  2. setuptools version 72.0.0 (released July 28, 2024) removed this deprecated module.
  3. Most Python environments automatically installed setuptools 72.0.0 when it was available.
<warning> **This is not a pip issue** but a compatibility problem between the package and new setuptools releases. </warning>

Solutions

bash
pip install --upgrade pip
pip install RandomWords
<tip> **Why this works:** Setuptools 72.0.0 has been withdrawn ("yanked") from PyPI. Upgrading pip ensures you get setuptools 71.x automatically. </tip>

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
poetry add setuptools@<72
poetry install
<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>

Deep Dive: What Happened

  1. July 28, 2024: Setuptools 72.0.0 released with breaking changes (setuptools.command.test removed)
  2. July 29-30: Widespread installation failures across Python ecosystem
  3. 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

  1. Pin setuptools in critical projects:

    python
    # pyproject.toml (Poetry example)
    [tool.poetry.dependencies]
    setuptools = "&lt;72"
  2. Use constraints in CI/CD pipelines

    yaml
    # GitHub Actions example
    - name: Install dependencies
      env:
        PIP_CONSTRAINT: constraints.txt
      run: pip install -r requirements.txt
  3. Verify dependencies:

    bash
    pip list | grep setuptools
    # Should show version <=71.x
<tip> **For package maintainers**: Replace `from setuptools.command.test import test` with modern testing integrations like `pytest` to avoid compatibility breaks. </tip>