Pip
Pip is the Python package installer that allows you to install and manage Python packages from the Python Package Index (PyPI) and other repositories.
Installation
# Check if pip is installed
pip --version
# Install pip (Python 3)
python -m ensurepip --upgrade
# Upgrade pip
pip install --upgrade pip
# On macOS
python3 -m pip install --upgrade pip
# On Windows
python -m pip install --upgrade pip
Basic Commands
# Install package
pip install requests
# Install specific version
pip install requests==2.31.0
# Install with version constraint
pip install requests>=2.28.0,<3.0
# Install multiple packages
pip install requests flask django
# List installed packages
pip list
# Show package information
pip show requests
# Search for package (deprecated, use PyPI.org)
pip search flask
Package Management
# Upgrade package
pip install --upgrade requests
# Uninstall package
pip uninstall requests
# Uninstall without confirmation
pip uninstall -y requests
# Download without installing
pip download requests
# Check for outdated packages
pip list --outdated
# Freeze requirements
pip freeze > requirements.txt
# Show where package is installed
pip show -f requests
Requirements Files
Create requirements.txt
# Generate from installed packages
pip freeze > requirements.txt
# Install from file
pip install -r requirements.txt
# Install with constraints
pip install -r requirements.txt --constraint constraints.txt
# Upgrade all packages
pip install -r requirements.txt --upgrade
requirements.txt Format
# Project dependencies
requests==2.31.0
flask==3.0.0
sqlalchemy==2.0.23
python-dotenv==1.0.0
# Comments can be added with #
# Version specifiers
django>=4.0,<5.0
numpy~=1.24.0
# Local path
-e ./local_package
# URL
git+https://github.com/user/repo.git#egg=package
# Extras
requests[security]==2.31.0
Version Specifiers
|Operator|Example|Meaning|
|---------|-------------|
|==|requests==2.31.0|Exact version|
|>=|requests>=2.28.0|Greater or equal|
|<=|requests<=3.0|Less or equal|
|>|requests>2.28.0|Greater than|
|<|requests<3.0|Less than|
|~=|requests~=2.31|Compatible version|
|===|requests===2.31.0|Arbitrary equality|
Virtual Environments
# Create virtual environment
python -m venv venv
# Activate (Linux/macOS)
source venv/bin/activate
# Activate (Windows)
venv\Scripts\activate
# Deactivate
deactivate
# Install in virtual environment
pip install -r requirements.txt
# Freeze virtual environment
pip freeze > requirements.txt
Advanced Usage
Install from Different Sources
# Install from PyPI (default)
pip install requests
# Install from URL
pip install https://github.com/user/repo/archive/main.zip
# Install from Git
pip install git+https://github.com/user/repo.git
# Install from local file
pip install ./local_package/
# Install in editable mode (development)
pip install -e ./local_package/
Managing Installation
# Install with no dependencies
pip install --no-deps requests
# Install with specific Python version
pip install -p /usr/bin/python3.11 requests
# Install to specific directory
pip install -t /path/to/dir requests
# Dry run (show what would be installed)
pip install --dry-run requests
# Quiet output
pip install -q requests
# Verbose output
pip install -v requests
Troubleshooting
# Check configuration
pip config list
# Set pip configuration
pip config set global.index-url https://pypi.org/simple/
# Clear pip cache
pip cache purge
# List cached packages
pip cache list
# Remove specific cached package
pip cache remove requests
# Check for issues
pip check
# Show installed packages with details
pip list -v
# Find where pip is installed
pip show pip
Common Issues
Upgrade pip installation
# Get pip version
python -m pip --version
# Upgrade pip
python -m pip install --upgrade pip
# Specific version
python -m pip install pip==23.0
Permission Denied
# Install for current user
pip install --user requests
# Or use virtual environment (recommended)
python -m venv venv
source venv/bin/activate
pip install requests
Module Not Found
# Check if package is installed
pip show requests
# Reinstall package
pip install --force-reinstall requests
# Check Python path
python -c "import sys; print(sys.path)"
CI/CD Integration
GitHub Actions
name: Install Dependencies
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- run: pytest tests/
requirements-dev.txt
# Create separate files for different environments
-r requirements.txt
# Development only
pytest==7.4.0
pytest-cov==4.1.0
black==23.7.0
flake8==6.0.0
Best Practices
- Use virtual environments for each project
- Pin exact versions in requirements.txt
- Review package updates regularly
- Check for security vulnerabilities with
pip audit - Keep pip and setuptools updated
- Use requirements files for reproducibility
- Document Python version requirements
- Use
.gitignoreto exclude venv directories
Resources
Last updated: 2025-07-06|Edit on GitHub