Python Environments
Choosing an Environment Manager
Section titled “Choosing an Environment Manager”For deep learning projects, you need isolated Python environments to manage dependencies. Here are your options:
Recommended: venv + pip
Section titled “Recommended: venv + pip”Best for most users - More stable and predictable
Pros:
- Built into Python (no extra installation)
- Works seamlessly with pip
- Lighter weight than Conda
- Fewer dependency conflicts
- Faster environment creation
Cons:
- No cross-language packages (R, C++, etc.)
- Manual CUDA/system library management
Alternative: Conda
Section titled “Alternative: Conda”Best for: Complex scientific computing stacks with system dependencies
Pros:
- Manages system libraries (CUDA, cuDNN, etc.)
- Cross-language support
- Pre-built binaries for many packages
Cons:
- Larger disk usage
- Slower environment solving
- Mixing pip and Conda can cause conflicts
- More complex to troubleshoot
Quick Start
Section titled “Quick Start”Create Environment
Section titled “Create Environment”# Create virtual environment
python3 -m venv ml-env
# Activate it
source ml-env/bin/activate # Linux/Mac
# or
ml-env\Scripts\activate # Windows
# Upgrade pip
pip install --upgrade pipInstall Packages
Section titled “Install Packages”# Install PyTorch with CUDA
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# Or TensorFlow
pip install tensorflow[and-cuda]
# Install common ML packages
pip install numpy pandas matplotlib scikit-learn jupyterDeactivate
Section titled “Deactivate”deactivateList All Environments
Section titled “List All Environments”# Find all venv environments
find ~ -type d -name "bin" -exec test -e "{}/activate" \; -print 2>/dev/nullDelete Environment
Section titled “Delete Environment”# Simply delete the folder
rm -rf ml-envCreate Environment
Section titled “Create Environment”# Create environment with specific Python version
conda create -n ml-env python=3.11
# Activate it
conda activate ml-envInstall Packages
Section titled “Install Packages”# IMPORTANT: Install conda packages FIRST, then pip packages
# Install CUDA toolkit via Conda (if needed)
conda install cuda -c nvidia
# Install PyTorch via Conda
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
# Or use pip for PyTorch (after conda packages)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121Deactivate
Section titled “Deactivate”conda deactivateList Environments
Section titled “List Environments”conda env listDelete Environment
Section titled “Delete Environment”conda env remove -n ml-envEnvironment Management Best Practices
Section titled “Environment Management Best Practices”Project-Specific Environments
Section titled “Project-Specific Environments”Create one environment per project:
# Project 1
python3 -m venv ~/envs/project1
source ~/envs/project1/bin/activate
pip install torch transformers
# Project 2
python3 -m venv ~/envs/project2
source ~/envs/project2/bin/activate
pip install tensorflow kerasRequirements Files
Section titled “Requirements Files”Track dependencies:
# Save current packages
pip freeze > requirements.txt
# Install from requirements
pip install -r requirements.txtBetter: Use minimal requirements.txt
# requirements.txt - only specify what you actually use
torch>=2.0.0
transformers>=4.30.0
numpy>=1.24.0Environment Aliases
Section titled “Environment Aliases”Add to ~/.bashrc or ~/.zshrc:
# Quick activation aliases
alias ml='source ~/envs/ml-env/bin/activate'
alias cv='source ~/envs/cv-project/bin/activate'
alias nlp='source ~/envs/nlp-project/bin/activate'Common Issues
Section titled “Common Issues”Issue: Conda Taking Forever to Solve Environment
Section titled “Issue: Conda Taking Forever to Solve Environment”Problem: Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solutions:
# Install mamba (drop-in replacement for conda)
conda install mamba -c conda-forge
# Use mamba instead of conda
mamba install pytorch torchvision -c pytorchMamba uses a faster dependency solver than conda.
# Create conda environment
conda create -n ml-env python=3.11
conda activate ml-env
# Use pip for packages (much faster)
pip install torch torchvisionAvoids conda’s slow dependency solving.
Issue: Mixing pip and Conda Broke Environment
Section titled “Issue: Mixing pip and Conda Broke Environment”Problem: Packages installed but not found, dependency conflicts
Solution: Start fresh with one approach
# Delete broken environment
conda env remove -n broken-env
# Recreate with venv + pip only
python3 -m venv ml-env
source ml-env/bin/activate
pip install -r requirements.txtIssue: “No module named X” After Installation
Section titled “Issue: “No module named X” After Installation”Check you’re in the right environment:
# Check which Python is being used
which python
# Should show your environment path, not system Python
# Correct: /home/user/ml-env/bin/python
# Wrong: /usr/bin/pythonIssue: Multiple Python Versions Conflicting
Section titled “Issue: Multiple Python Versions Conflicting”Use specific Python version:
# venv with specific Python
python3.11 -m venv ml-env
source ml-env/bin/activate# Conda with specific Python
conda create -n ml-env python=3.11
conda activate ml-envConda-Specific Issues
Section titled “Conda-Specific Issues”Check Conda Disk Usage
Section titled “Check Conda Disk Usage”# Check conda cache size
du -hs ~/.conda
# Clean up old packages
conda clean --all
# Remove unused environments
conda env list
conda env remove -n unused-envTensorFlow Version Conflicts
Section titled “TensorFlow Version Conflicts”# Search for compatible versions
conda search -c conda-forge tensorflow-gpu
# Install specific version
conda install -c conda-forge tensorflow-gpu=2.16.2Jupyter Integration
Section titled “Jupyter Integration”See Jupyter Notebooks for:
- Adding environments as Jupyter kernels
- Managing ipykernel
- Jupyter-specific troubleshooting
Quick Reference
Section titled “Quick Reference”venv Commands
Section titled “venv Commands”# Create
python3 -m venv envname
# Activate
source envname/bin/activate
# Install package
pip install packagename
# Save dependencies
pip freeze > requirements.txt
# Deactivate
deactivate
# Delete
rm -rf envnameConda Commands
Section titled “Conda Commands”# Create
conda create -n envname python=3.11
# Activate
conda activate envname
# Install package
conda install packagename
# Save dependencies
conda env export > environment.yml
# Deactivate
conda deactivate
# Delete
conda env remove -n envnameNext Steps
Section titled “Next Steps”- Set up Jupyter notebooks with your environment
- Install NVIDIA drivers for GPU support
- Troubleshoot GPU errors