Skip to content

06. pip

Overview

pip is the standard package installer for Python, used to install, upgrade, remove, and manage Python packages from package indexes such as the Python Package Index (PyPI). It is the default tool for dependency management in most Python environments and is commonly used in local development, virtual environments, containers, and CI/CD pipelines.

Primary use cases include:

(1) Installing third-party libraries from PyPI

(2) Managing project dependencies via requirement files

(3) Upgrading or removing installed packages

(4) Inspecting installed distributions and their metadata

pip is designed to work across platforms and integrates with Python’s packaging ecosystem, including wheels, virtual environments, and package indexes.

Installation and Scope

Default Availability

In most modern Python distributions, pip is included by default.

(1) Python 3.4+ includes pip through the ensurepip module

(2) Python installers for Windows and macOS typically install pip automatically

(3) Many Linux distributions provide pip through system package managers

Environment Scope

pip can operate in different installation scopes.

Scope Description Typical Use Case
System-wide Installs packages into the global Python environment OS-managed environments, base containers
User site Installs packages into the user’s home directory Restricted environments without root access
Virtual environment Installs packages isolated from the system Python Project-specific dependencies

Recommended installation practice

When working on application or library development:

(1) Create a virtual environment using python -m venv <env>

(2) Activate the environment

(3) Install dependencies using pip inside the virtual environment

This prevents dependency conflicts and avoids modifying the system Python.

Why pip Became the Standard Tool

pip became the dominant package installer in the Python ecosystem due to technical, ecosystem, and governance factors.

(1) Official endorsement

(1.1) pip is recommended in the official Python documentation as the standard installation tool.

(1.2) It is bundled with modern Python releases, increasing adoption by default.

(2) Integration with PyPI

(2.1) pip integrates directly with the Python Package Index (PyPI).

(2.2) It supports both public and private package indexes.

(3) Wheel support

(3.1) Native support for wheel distributions enables fast, reliable installations.

(3.2) Binary wheels reduce the need for local compilation.

(4) Dependency resolution (4.1) Modern versions include a robust dependency resolver. (4.2) It enforces compatibility constraints declared by packages.

(5) Ecosystem compatibility (5.1) Supported by most Python tools, frameworks, and cloud platforms. (5.2) Widely integrated into CI/CD systems and container workflows.

(6) Extensibility and configuration (6.1) Supports configuration files and environment variables. (6.2) Allows integration with internal artifact repositories.

Adoption implication

For most Python-based systems:

(1) Assume pip is available in the runtime environment

(2) Standardize dependency management around pip and requirements.txt

(3) Validate compatibility when using alternative package managers

Core Concepts and Components

Key Concepts

Component Description
Package A distributable Python project, typically published on PyPI
Distribution A packaged release of a project (wheel or source archive)
Wheel A built distribution format (.whl) designed for fast installation
Requirement A specification of a package and optional version constraints
Package index A repository of packages, such as PyPI

Supported Distribution Types

Type File Extension Description
Wheel .whl Prebuilt binary distribution; preferred by pip
Source distribution .tar.gz, .zip Requires local build during installation

pip Commands Reference

pip provides 14 primary commands for package management and environment inspection.

Command Description
install Install package
uninstall Uninstall package
inspect Inspect package
list List installed packages
show Show information about package
freeze Output installed packages in requirements format
check Verify installed packages have compatible dependencies
download Download package
wheel Build wheel archives for a package and all of its dependencies
hash Compute hashes of package archives
search Search PyPI for packages whose name or summary matches the given terms
cache Inspect and manage pip's wheel cache
config Manage pip's configuration
debug Show information useful for debugging pip

Search command behavior

The search command depends on PyPI’s XML-RPC API and may not be supported by all package indexes.

Before relying on pip search:

(1) Verify that the target index supports search functionality

(2) Consider searching directly on the package index website

Package Discovery and Installation Flow

This flow describes how pip locates and installs a package. It is triggered when a user executes a command such as pip install <package>, and it results in the package being downloaded and installed into the target environment.

High-Level Flow (pip and Package Index)

flowchart LR
    A[User: pip install package] --> B[pip client]
    B --> C[Package Index<br>PyPI or custom index]
    C --> D[Package metadata]
    D --> E[Select compatible distribution]
    E --> F[Download wheel or source]
    F --> G[Install into environment]

Package Search and Selection Steps

(1) The user runs pip install <package> in a Python environment. (2) pip reads configuration, including index URLs and authentication settings. (3) pip sends a request to the configured package index (default: PyPI). (4) The index returns package metadata and available distributions. (5) pip evaluates version constraints and environment compatibility. (6) pip selects the most suitable distribution, preferring wheels when available. (7) The selected distribution is downloaded to the local cache. (8) If the distribution is a wheel, it is installed directly. (9) If the distribution is a source archive, it is built into a wheel before installation. (10) The package and its dependencies are installed into the target environment.

Index and trust configuration

When installing from external package indexes:

(1) Verify the package index URL is trusted

(2) Use HTTPS connections for all package sources

(3) Consider enabling hash-checking mode for reproducible and secure installs

Limitations and Constraints

(1) pip does not provide full environment management; it relies on tools such as venv or virtualenv.

(2) Dependency resolution may fail if incompatible version constraints are specified.

(3) System-wide installations may conflict with OS package managers.

(4) Source distributions require build tools and compilers on the host system.

(5) Some packages may provide wheels only for specific platforms or Python versions.

System Python considerations

On Linux or managed systems:

(1) Avoid installing packages into the system Python using sudo pip install

(2) Use virtual environments for application dependencies

(3) Use the system package manager for OS-level Python packages

Reference

(1) https://docs.python.org/3/installing/index.html

(2) https://pip.pypa.io/en/stable/topics/secure-installs/

(3) https://realpython.com/python-wheels/