Python Package Management and pip
Mastering package installation, dependency management, and professional workflows
Table of Contents
1. pip Installation Deep Dive
Installation Methods
Method | Command | Use Case |
---|---|---|
Default install | python -m pip install package |
Most reliable (avoids PATH issues) |
User install | pip install --user package |
No admin rights needed |
Editable mode | pip install -e . |
Development/local packages |
From source | pip install git+https://repo.url |
Direct from VCS |
From archive | pip install package.tar.gz |
Local/offline installs |
Version Control
Pre-release versions
pip install --pre package # Gets alpha/beta/rc versions
Version specifiers
~=1.2.3
(1.2.3+, but <1.3.0)!=1.2.3
(exclude specific version)>=1.0.0,<2.0.0
(version range)
Advanced Flags
--no-deps |
Skip dependency installation |
--ignore-installed |
Force reinstall |
--prefix |
Custom installation path |
--no-cache-dir |
Disable caching |
--compile |
Compile Python source to bytecode |
Pro Tip: Always use python -m pip
instead of direct pip
command to avoid PATH issues and ensure you're using the correct Python environment's pip.
2. Requirements Files (Enhanced)
File Formats
# Standard requirements.txt
package==1.0.0
another-package>=2.0.0
# With hashes (secure)
package==1.0.0 \
--hash=sha256:abc123... \
--hash=sha256:def456...
Advanced Features
Constraints files
# constraints.txt
package==1.0.0
another-package>=2.0.0
# Usage
pip install -c constraints.txt package
Locks versions without forcing installs
Multi-stage requirements
requirements/
├── base.txt
├── dev.txt # -r base.txt + testing tools
└── prod.txt # -r base.txt + gunicorn
├── base.txt
├── dev.txt # -r base.txt + testing tools
└── prod.txt # -r base.txt + gunicorn
Environment markers
pytest; python_version > "3.8" and sys_platform == "linux"
Generation Tools
pip freeze > requirements.txt
(basic snapshot)pipreqs
(scans imports to generate minimal requirements)pip-compile
(from pip-tools for deterministic builds)
Warning: pip freeze
captures all installed packages, including transitive dependencies. For production, prefer manually curated requirements or use pip-compile
.
3. Virtual Environments (Professional Setup)
Comparison of Tools
Tool | Command | Best For |
---|---|---|
venv | python -m venv .venv |
Standard library (Python 3.3+) |
virtualenv | virtualenv venv |
Legacy Python support |
conda | conda create -n env |
Data science stacks |
pipenv | pipenv shell |
Combined dep management |
Advanced venv Usage
Custom Python versions
python3.9 -m venv py39-env
Without pip
python -m venv --without-pip env
Relocatable envs
python -m venv --relocatable .venv
Automation & Integration
Shell auto-activation
# Add to .bashrc/zshrc
venv() { python -m venv "$1" && source "$1/bin/activate" }
VS Code integration
// .vscode/settings.json
{
"python.venvPath": "./.venv",
"python.defaultInterpreterPath": "./.venv/bin/python"
}
4. Professional Workflow Integration
CI/CD Pipeline Example
# .github/workflows/test.yml
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Create venv
run: python -m venv .venv
- name: Install dependencies
run: |
source .venv/bin/activate
pip install -r requirements.txt
pip install pytest
- name: Run tests
run: |
source .venv/bin/activate
pytest
Security Best Practices
Vulnerability scanning
pip install safety
safety check
Hash verification
# requirements.txt
package==1.0.0 \
--hash=sha256:abc123...
Dependency auditing
pip install pip-audit
pip-audit
Security Tip: Always verify package hashes for production deployments to prevent supply chain attacks.
5. Troubleshooting Guide
Issue | Solution | Prevention |
---|---|---|
Broken dependencies | pip check |
Use hash verification |
Permission errors | --user flag |
Use virtualenvs |
SSL errors | pip --trusted-host pypi.org |
Update certs |
Slow installs | --find-links + local mirror |
Set up local cache |
Version conflicts | pipdeptree to analyze |
Pin versions in requirements |
6. Next-Level Recommendations
Dependency isolation
pip install --target ./lib package
export PYTHONPATH=./lib
Wheel building
pip wheel -w wheels/ -r requirements.txt
pip install --no-index --find-links=wheels/ -r requirements.txt
Package indexes
Configure multiple indexes in pip.conf:
[global]
extra-index-url = https://internal.example.com/simple
Additional Tools
- poetry: Modern dependency management and packaging
- pipx: Install Python applications in isolated environments
- pdm: Python package and dependency manager supporting PEP 582