The PyPA recommended tool for installing Python packages.
91
The PyPA recommended tool for installing Python packages. Pip is the standard package manager for Python that installs packages from the Python Package Index (PyPI) and other repositories. It provides comprehensive package management capabilities including installation, uninstallation, dependency resolution, and package information queries.
IMPORTANT: Pip is designed as a command-line tool only and explicitly provides no public Python API for programmatic use. All functionality must be accessed through the CLI interface or subprocess calls.
pip, pip3, python -m pip# Direct command usage
pip [command] [options]
pip3 [command] [options]
# Module execution
python -m pip [command] [options]Since pip provides no public Python API, use subprocess for programmatic package management:
import subprocess
import sys
# Install a package
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'requests'])
# Uninstall a package
subprocess.check_call([sys.executable, '-m', 'pip', 'uninstall', 'requests', '-y'])
# List installed packages
result = subprocess.run([sys.executable, '-m', 'pip', 'list'],
capture_output=True, text=True)
print(result.stdout)# Install packages
pip install package_name
pip install package_name==1.2.3
pip install -r requirements.txt
pip install git+https://github.com/user/repo.git
# Uninstall packages
pip uninstall package_name
pip uninstall -r requirements.txt
# List installed packages
pip list
pip list --outdated
# Show package information
pip show package_name
# Generate requirements file
pip freeze > requirements.txt
# Upgrade packages
pip install --upgrade package_name
pip install --upgrade-strategy eager -r requirements.txtPip follows a command-based architecture where each operation is handled by a specific command class:
All implementation details are contained within the pip._internal.* namespace and are explicitly marked as private APIs subject to change without notice.
Install Python packages from PyPI, version control systems, local files, and other sources with comprehensive dependency resolution and conflict handling.
# Basic installation
pip install package_name
# Install specific version
pip install package_name==1.2.3
# Install from requirements file
pip install -r requirements.txt
# Install from git repository
pip install git+https://github.com/user/repo.git
# Install in development mode
pip install -e .Uninstall packages, list installed packages, show package information, and manage package metadata with comprehensive querying capabilities.
# Uninstall packages
pip uninstall package_name
# List packages
pip list
pip list --outdated
# Show package details
pip show package_name
# Check for dependency conflicts
pip checkBuild wheels, compute package hashes, and manage distribution formats for package development and deployment workflows.
# Build wheels
pip wheel .
pip wheel -r requirements.txt
# Compute package hashes
pip hash package.tar.gz
# Download without installing
pip download package_nameManage pip configuration, inspect Python environment information, handle caching, and configure package indexes for customized package management workflows.
# Configuration management
pip config list
pip config set global.index-url https://pypi.org/simple/
# Environment inspection
pip inspect
# Cache management
pip cache info
pip cache purge
# Index operations
pip index versions package_name
# Help and completion
pip help
pip completion --bashPip explicitly provides no public Python API. From the official documentation:
"pip is a command line program. While it is implemented in Python, and so is available from your Python code via import pip, you must not use pip's internal APIs in this way."
Key points:
pip is subject to change# Version information (most stable import)
import pip
print(pip.__version__) # "24.3.1"
# Deprecated main function (internal use only)
from pip import main # DO NOT USE - for console scripts onlyFor programmatic package management needs, consider these alternatives:
Pip exits with non-zero status codes on errors:
import subprocess
import sys
try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'nonexistent-package'])
except subprocess.CalledProcessError as e:
print(f"Pip command failed with exit code {e.returncode}")
print(f"Command: {' '.join(e.cmd)}")Common exit codes:
0: Success1: General error2: Command parsing errorInstall with Tessl CLI
npx tessl i tessl/pypi-pipevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10