cheat-sheets

pip and pipx

pip installs Python packages into an environment; pipx installs Python command-line tools, each in its own isolated virtual environment, and puts the commands on your PATH. Use pip inside a virtual environment for project dependencies, and pipx for tools such as ansible or yt-dlp. For the faster all-in-one alternative see Python and uv.

Documentation: pip · pipx Project: pypa/pip · pypa/pipx

Verification: options checked against pip --help for pip 24.0 and pipx --help for pipx 1.18.15 in a throwaway python:3.11-slim container. pipx install, list, run, inject, runpip, upgrade and environment, and pip freeze, list --outdated, check, show, config list, download, hash and inspect were run. Nothing was installed on the host. pipx flags change between releases (the newest ones are --cooldown and --backend); run pipx <COMMAND> --help for yours.

Related sheets: Python and uv, pre-commit, Ansible Cheat-Sheet, yt-dlp.

Environments First #

On Debian and Ubuntu the system Python is marked externally managed, so pip install outside a virtual environment is refused. Use a venv.

COMMANDDESCRIPTION
python3 -m venv .venvCreate a virtual environment
source .venv/bin/activateActivate it (deactivate to leave)
python -m pip ...Run the pip belonging to that interpreter (safest form)
sudo apt install python3-venvNeeded on Debian and Ubuntu if venv is missing
pip install --break-system-packages <PACKAGE>Override the external-management guard (avoid; can break OS tools)
pip --require-virtualenv install <PACKAGE>Refuse to run outside a virtual environment
PIP_REQUIRE_VIRTUALENV=trueSame, as an environment variable

Install and Remove #

COMMANDDESCRIPTION
pip install <PACKAGE>Install the latest version
pip install <PACKAGE>==<VERSION>Exact version
pip install '<PACKAGE>>=1.2,<2'Version range (quote it)
pip install '<PACKAGE>[<EXTRA>]'Install with an extra
pip install -U <PACKAGE>Upgrade (--upgrade)
pip install --force-reinstall <PACKAGE>Reinstall even if current
pip install -r requirements.txtInstall from a file
pip install -c constraints.txt -r requirements.txtApply version limits without adding packages
pip install -e .Editable install of the current project
pip install --no-deps <PACKAGE>Skip dependencies
pip install --pre <PACKAGE>Allow pre-releases
pip install --dry-run <PACKAGE>Show what would be installed
pip install --only-binary :all: <PACKAGE>Refuse source builds (--no-binary forces them)
pip install --user <PACKAGE>Install for the current user only
pip install -t <DIR> <PACKAGE>Install into a target directory
pip install --require-hashes -r requirements.txtRequire a --hash for every entry; repeatable and tamper-evident
pip install --report <FILE> <PACKAGE>Write a JSON install report (use - for stdout)
pip uninstall <PACKAGE> / pip uninstall -y -r requirements.txtRemove, optionally without prompts

Inspect #

COMMANDDESCRIPTION
pip listInstalled packages (--outdated, --not-required, --format json, --local)
pip freezeInstalled packages in requirements format (--exclude-editable, --all)
pip freeze > requirements.txtPin the current environment
pip show <PACKAGE>Metadata (-f lists installed files)
pip checkVerify dependencies are consistent
pip inspectJSON description of the environment
pip debugVersion, interpreter and compatible-tag details for bug reports
pip --versionpip version and location
pip index versions <PACKAGE>List versions available on the index

pip search is still listed in --help but PyPI does not support it; search on pypi.org.

Download and Build #

COMMANDDESCRIPTION
pip download -d <DIR> -r requirements.txtDownload distributions without installing (for offline installs)
pip install --no-index --find-links <DIR> -r requirements.txtInstall from that directory only
pip wheel -w <DIR> -r requirements.txtBuild wheels for your requirements
pip hash <FILE>Print a --hash=sha256:... line for a downloaded file
pip download --platform <TAG> --only-binary :all: --python-version <VER> <PACKAGE>Fetch wheels for another platform (see pip download --help)

Indexes, Config and Cache #

COMMANDDESCRIPTION
pip install -i <URL> <PACKAGE>Use another index (--index-url)
pip install --extra-index-url <URL> <PACKAGE>Also search another index (a dependency-confusion risk; prefer a single index)
pip install -f <DIR_OR_URL> <PACKAGE>Look in a directory or page of archives (--find-links)
pip config listShow active configuration
pip config debugShow config files and where each value comes from
pip config set global.index-url <URL>Set an option in the user config
pip config get global.index-urlRead it
pip config unset global.index-urlRemove it
pip config edit --editor <EDITOR>Edit the config file (--user, --global, --site pick the file)
pip cache dir / pip cache infoShow the cache location and size
pip cache listList cached wheels
pip cache remove '<PATTERN>' / pip cache purgeRemove some or all cached wheels
PIP_NO_CACHE_DIR=1Disable the cache (then pip cache commands fail)

Config lives in pip.conf (Linux) under the user or global config directory; pip config debug lists the exact paths.

pipx: Install and Run Tools #

COMMANDDESCRIPTION
sudo apt install pipxInstall on Debian and Ubuntu (or python3 -m pip install --user pipx)
pipx ensurepathAdd pipx’s bin directory to PATH (open a new shell afterwards)
pipx install <PACKAGE>Install a tool in its own venv
pipx install '<PACKAGE>==<VERSION>'Install a specific version
pipx install --python <PYTHON> <PACKAGE>Use a particular interpreter
pipx install --suffix <@SUFFIX> <PACKAGE>Install a second copy side by side
pipx install --include-deps <PACKAGE>Also expose the dependencies’ apps
pipx install --pip-args '<ARGS>' <PACKAGE>Pass options through to pip (for example an index URL)
pipx install --global <PACKAGE>System-wide install (--global works on most commands)
pipx install git+https://example.com/<OWNER>/<REPO>.gitInstall from a Git URL
pipx run <PACKAGE> <ARGS>Run once in a temporary environment, no install
pipx run --spec '<PACKAGE>==<VERSION>' <COMMAND>Run a command from a specific package version
pipx run --with <EXTRA_PACKAGE> <PACKAGE>Add extra packages to the temporary environment
pipx run --no-cache <PACKAGE>Do not reuse a cached temporary environment
pipx exec <APP>Run an app from an already installed environment

pipx: Manage Tools #

COMMANDDESCRIPTION
pipx listInstalled tools and their apps (--short, --json, --outdated, --include-injected)
pipx upgrade <PACKAGE>Upgrade one tool
pipx upgrade-allUpgrade every tool
pipx reinstall <PACKAGE> / pipx reinstall-allRebuild after a Python upgrade
pipx uninstall <PACKAGE> / pipx uninstall-allRemove
pipx inject <TOOL> <EXTRA_PACKAGE>Add a package to a tool’s venv (a plugin, for example)
pipx uninject <TOOL> <EXTRA_PACKAGE>Remove it
pipx runpip <TOOL> <PIP_ARGS>Run pip inside the tool’s venv (pipx runpip <TOOL> list)
pipx pin <PACKAGE> / pipx unpin <PACKAGE>Stop or resume upgrades for a tool
pipx health / pipx repairCheck, or fix, broken tool environments
pipx interpreter listPython interpreters pipx manages
pipx cacheManage cached run environments
pipx environmentShow the variables and paths pipx uses
pipx completionsShow how to enable shell completion

pipx: Paths #

VARIABLEDESCRIPTION
PIPX_HOMEWhere the tool venvs live
PIPX_BIN_DIRWhere the commands are linked (must be on PATH)
PIPX_MAN_DIRWhere man pages are linked
PIPX_GLOBAL_HOME / PIPX_GLOBAL_BIN_DIRThe same for --global installs

Common Tasks #

Reproducible dependencies for a project:

bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python -m pip freeze --exclude-editable > requirements.lock
python -m pip check

Offline install from a directory of downloads:

bash
python -m pip download -d ./wheelhouse -r requirements.txt
python -m pip install --no-index --find-links ./wheelhouse -r requirements.txt

Troubleshooting #

SYMPTOMFIX
externally-managed-environment errorUse a venv or pipx; avoid --break-system-packages
pip: command not foundUse python3 -m pip, or sudo apt install python3-pip
Tool installed with pipx is not foundRun pipx ensurepath and start a new shell
Tools break after a Python upgradepipx reinstall-all
Build fails for a package with C codeInstall the compiler and headers (Debian: build-essential python3-dev), or use --only-binary :all:
Two tools need different versions of a libraryInstall each with pipx, not into one shared venv
pip installs into the wrong PythonRun python -m pip, and check with pip --version

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.