Browse topics

Installing packages with pip

Version
3.14
Review
Draft

Summary#

python -m pip install runs pip with the selected Python interpreter. Use the interpreter in your project's virtual environment to install individual packages or a requirements file.

Syntax#

With your virtual environment active, install one package or the packages listed in a requirements file. Add --no-index --find-links to install only from a local directory.

sh
python -m pip install package_name==1.0.0
python -m pip install -r requirements.txt
python -m pip install --no-index --find-links ./wheels -r requirements.txt

Example#

This example runs offline. It writes a tiny wheel, installs it from that directory into a temporary environment and prints the installed version. It suppresses pip's install messages, which can vary.

python
import os
from pathlib import Path
import subprocess
import sys
from tempfile import TemporaryDirectory
from zipfile import ZipFile

INFO = "greeting-1.0.0.dist-info"
FILES = {
    "greeting.py": 'MESSAGE = "hello"\n',
    f"{INFO}/METADATA": "Metadata-Version: 2.1\nName: greeting\nVersion: 1.0.0\n",
    f"{INFO}/WHEEL": "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
}

with TemporaryDirectory() as directory:
    root = Path(directory)
    with ZipFile(root / "greeting-1.0.0-py3-none-any.whl", "w") as wheel:
        for name, content in FILES.items():
            wheel.writestr(name, content)
        record = "".join(f"{name},,\n" for name in [*FILES, f"{INFO}/RECORD"])
        wheel.writestr(f"{INFO}/RECORD", record)
    requirements = root / "requirements.txt"
    requirements.write_text("greeting==1.0.0\n", encoding="utf-8")

    env = dict(os.environ, PIP_CONFIG_FILE=os.devnull)
    env_dir = root / "env"
    subprocess.run([sys.executable, "-I", "-m", "venv", str(env_dir)], env=env, check=True)
    python = str(env_dir / ("Scripts/python.exe" if os.name == "nt" else "bin/python"))
    subprocess.run(
        [python, "-I", "-m", "pip", "--isolated", "--no-cache-dir",
         "--disable-pip-version-check", "install", "--no-index",
         "--find-links", str(root), "-r", str(requirements)],
        cwd=directory, env=env, check=True, stdout=subprocess.DEVNULL,
    )
    probe = ("from importlib.metadata import version; import greeting; "
             "print(greeting.MESSAGE, version('greeting'))")
    subprocess.run([python, "-I", "-c", probe], check=True)

Expected output#

Text
hello 1.0.0

Common mistakes#

A bare pip command can belong to a different interpreter. Use python -m pip with the same interpreter you use to run your code. == pins one distribution, so list dependency pins as well when you need to reproduce their versions.

error: externally-managed-environment means that the operating system or another package manager owns the selected interpreter and has marked it as off limits to pip. Create a virtual environment with python3 -m venv .venv, then run .venv/bin/python -m pip install package_name. The --break-system-packages option overrides the marker and can break software that depends on the system packages.

Version applicability#

The commands require pip in the selected interpreter. Python 3.4 and later normally include pip when creating a venv. The example's importlib.metadata requires Python 3.8 or later.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0