Introduction
pip is Python’s package installer โ the tool you use to install, upgrade, and manage third-party libraries. This guide covers everything from basic installation to virtual environments, dependency management, and troubleshooting.
Basic Commands
# Install a package
pip install requests
# Install a specific version
pip install requests==2.31.0
# Install minimum version
pip install "requests>=2.28.0"
# Install version range
pip install "requests>=2.28.0,<3.0.0"
# Upgrade a package
pip install --upgrade requests
# Uninstall a package
pip uninstall requests
# Uninstall without confirmation prompt
pip uninstall -y requests
Listing and Inspecting Packages
# List all installed packages
pip list
# List outdated packages
pip list --outdated
# Show package details (version, location, dependencies)
pip show requests
# Show package files
pip show --files requests
# Search PyPI (deprecated in newer pip, use pypi.org instead)
pip search requests
Requirements Files
Requirements files pin your dependencies for reproducible environments:
# Generate requirements.txt from current environment
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
# Install from requirements.txt with exact versions
pip install -r requirements.txt --require-hashes
requirements.txt Format
# requirements.txt
requests==2.31.0
numpy>=1.24.0,<2.0.0
pandas~=2.0.0 # ~= means compatible release (>=2.0.0, <3.0.0)
flask # no version constraint (latest)
pytest>=7.0 # minimum version
# Install from Git
git+https://github.com/scrapy/scrapyd-client.git
# Install from local path
-e ./my-local-package
# Include another requirements file
-r base-requirements.txt
Separating Dev and Production Dependencies
# requirements.txt (production)
flask==3.0.0
sqlalchemy==2.0.0
psycopg2-binary==2.9.9
# requirements-dev.txt (development only)
-r requirements.txt
pytest==7.4.0
black==23.0.0
ruff==0.1.0
mypy==1.5.0
# Install production deps
pip install -r requirements.txt
# Install dev deps (includes production)
pip install -r requirements-dev.txt
Virtual Environments
Always use virtual environments to isolate project dependencies:
# Create a virtual environment
python3 -m venv .venv
# Activate (Linux/macOS)
source .venv/bin/activate
# Activate (Windows)
.venv\Scripts\activate
# Deactivate
deactivate
# Verify you're in the venv
which python # should show .venv/bin/python
pip list # shows only packages in this venv
.gitignore for Virtual Environments
# .gitignore
.venv/
venv/
env/
__pycache__/
*.pyc
Installing from Different Sources
# From PyPI (default)
pip install requests
# From a Git repository
pip install git+https://github.com/psf/requests.git
# From a specific branch/tag/commit
pip install git+https://github.com/psf/[email protected]
pip install git+https://github.com/psf/requests.git@main
# From a local directory (editable install โ changes reflected immediately)
pip install -e ./my-package
pip install -e . # install current directory as editable
# From a wheel file
pip install requests-2.31.0-py3-none-any.whl
# From a tarball
pip install requests-2.31.0.tar.gz
# From a URL
pip install https://example.com/packages/mypackage.tar.gz
pip Configuration
# Show current configuration
pip config list
# Set a configuration value
pip config set global.index-url https://pypi.org/simple/
# Use a different package index (e.g., company internal)
pip install mypackage --index-url https://internal.example.com/simple/
# Use an extra index alongside PyPI
pip install mypackage --extra-index-url https://internal.example.com/simple/
# Disable SSL verification (not recommended)
pip install requests --trusted-host pypi.org --trusted-host files.pythonhosted.org
pip.conf / pip.ini
# ~/.pip/pip.conf (Linux/macOS)
# %APPDATA%\pip\pip.ini (Windows)
[global]
index-url = https://pypi.org/simple/
timeout = 60
trusted-host = pypi.org
files.pythonhosted.org
[install]
require-virtualenv = true # refuse to install outside a venv
Dependency Resolution and Conflicts
# Check for dependency conflicts
pip check
# Install with verbose output to debug issues
pip install -v requests
# Force reinstall
pip install --force-reinstall requests
# Install without dependencies
pip install --no-deps requests
# Download packages without installing (for offline use)
pip download requests -d ./packages/
pip install --no-index --find-links ./packages/ requests
Modern Alternatives to pip
pip + venv (Standard)
The built-in combination โ works everywhere, no extra tools needed.
pipenv
Combines pip and venv with a Pipfile for cleaner dependency management:
pip install pipenv
pipenv install requests
pipenv install pytest --dev
pipenv shell # activate venv
Poetry
Modern dependency management with lock files and publishing:
pip install poetry
poetry new myproject
poetry add requests
poetry add pytest --group dev
poetry install
poetry shell
uv (Fastest โ 2024+)
A Rust-based pip replacement that’s 10-100x faster:
pip install uv
uv pip install requests
uv pip install -r requirements.txt
uv venv
Troubleshooting
Permission Errors
# Don't use sudo with pip โ use --user instead
pip install --user requests
# Or better: use a virtual environment
python3 -m venv .venv && source .venv/bin/activate
pip install requests
SSL Certificate Errors
# Upgrade pip and certifi
pip install --upgrade pip certifi
# If behind a corporate proxy
pip install requests --cert /path/to/corporate-cert.pem
pip Not Found
# Python 3
python3 -m pip install requests
# Ensure pip is installed
python3 -m ensurepip --upgrade
Dependency Conflicts
# See what's conflicting
pip check
# Create a fresh environment and install from scratch
python3 -m venv .venv-fresh
source .venv-fresh/bin/activate
pip install -r requirements.txt
Quick Reference
| Command | Description |
|---|---|
pip install pkg |
Install latest version |
pip install pkg==1.0 |
Install specific version |
pip install -U pkg |
Upgrade package |
pip uninstall pkg |
Remove package |
pip list |
List installed packages |
pip list --outdated |
Show outdated packages |
pip show pkg |
Show package info |
pip freeze > req.txt |
Export dependencies |
pip install -r req.txt |
Install from file |
pip install -e . |
Editable install |
pip check |
Check for conflicts |
pip download pkg |
Download without installing |
Resources
- pip Documentation
- PyPI โ Python Package Index
- Python Packaging User Guide
- uv โ Fast Python Package Manager
Comments