Nanfeng

Notes on software development, code, and curious ideas

Repairing a Broken pip Installation

A pip command can fail before package installation begins when the launcher and installed pip package do not match, the environment is corrupted, or the wrong Python interpreter is being used.

Example pip traceback

Do not patch site-packages/pip/__main__.py by hand. That only masks a particular version mismatch and will be overwritten by the next upgrade.

Confirm the interpreter

1
2
python --version
python -m pip --version

Using python -m pip ties pip to the selected interpreter and avoids a stale pip executable elsewhere on PATH.

Repair pip

Try the standard library bootstrap first:

1
2
python -m ensurepip --upgrade
python -m pip install --upgrade pip setuptools wheel

If the project environment is disposable, recreating it is often faster and more reliable:

1
python -m venv .venv

Activate the environment using the command appropriate for the operating system, then run python -m pip install ... inside it.

If ensurepip is unavailable, use the operating system or Python distribution’s supported package mechanism. Avoid mixing a system-managed Python with globally installed pip packages, and do not use administrator privileges merely to silence a permissions error.

The original issue was observed with Python 3.7.2. That runtime and its old pip releases are now legacy; use a maintained Python version for new environments whenever project compatibility permits.

+