This guide covers managing Python package dependencies when you know the working versions.
# Generate requirements.txt with specific versions
pip freeze > requirements.txt
Your requirements.txt can include version specifiers:
torch==2.1.2 # Exact version
numpy>=1.20.0 # Minimum version
pandas~=1.4.0 # Compatible release (same as >=1.4.0, <1.5.0)
Always use virtual environments for project isolation:
# Create new virtual environment
python -m venv myproject_env
# Activate it (Unix/macOS)
source myproject_env/bin/activate
# OR Windows
myproject_env\Scripts\activate
Enhanced dependency management:
# Install pip-tools
pip install pip-tools
# Create requirements.in with direct dependencies
# Example requirements.in contents:
# torch
# xformers
# numpy
# Generate locked requirements.txt
pip-compile requirements.in
# Install from requirements.txt
pip-sync
# Check for conflicts
pip check
# Detailed dependency tree
pip install pipdeptree
pipdeptree -p torch # Check specific package
Document dependencies with their purposes:
# requirements.txt
torch==2.1.2 # Deep learning framework
torchvision==0.16.2 # Computer vision utilities
xformers==0.0.22.post7 # Memory-efficient transformers
For conda users:
# environment.yml
name: myproject
channels:
- pytorch
- conda-forge
dependencies:
- python=3.10
- torch=2.1.2
- torchvision=0.16.2
- xformers=0.0.22
Conda environment commands:
# Export environment
conda env export > environment.yml
# Create from file
conda env create -f environment.yml
This guide covers managing Python package dependencies when working with code that doesn't specify versions.
Follow this order for interdependent packages:
# 1. Core framework (e.g., PyTorch)
# 2. Framework extensions (e.g., torchvision)
# 3. Dependent packages (e.g., transformers)
# Check installed version
pip show package_name
# Check package dependencies
pip show package_name | grep Requires
# Detailed dependency tree
pip install pipdeptree
pipdeptree -p package_name
# If package_B installation fails:
# Check dependencies
pip show package_B
# Check installed versions
pip show package_A
# Try older version
pip install package_B==0.0.20
# Clean install in correct order
pip uninstall package_A package_B
pip install package_A==version
pip install package_B==version
import pkg_resources
def get_package_version(package_name):
try:
return pkg_resources.get_distribution(package_name).version
except pkg_resources.DistributionNotFound:
return "Not installed"
# Check versions
packages = ['torch', 'transformers', 'xformers']
for pkg in packages:
print(f"{pkg}: {get_package_version(pkg)}")
# Try requirements.txt if available
pip install -r requirements.txt
# Otherwise, systematic installation:
# 1. Check CUDA version
nvidia-smi
# 2. Install PyTorch for your CUDA version
pip install torch==2.1.2 torchvision==0.16.2 --index-url https://download.pytorch.org/whl/cu121
# 3. Install other packages
pip install transformers
pip install datasets
pip install xformers
# Debug installation
pip install package_name --verbose
pip install package_name== # Show available versions
- Check project's GitHub issues
- Note notebook's last update date
- Review major dependencies' release notes
- Try fresh virtual environment
- Consider using older framework versions
- Check repository's:
requirements.txt
setup.py
- Issue tracker
- Release dates
Remember to document working combinations once found!