Skip to content

Python Environments

For deep learning projects, you need isolated Python environments to manage dependencies. Here are your options:

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

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
# 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 pip
# 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 jupyter
deactivate
# Find all venv environments
find ~ -type d -name "bin" -exec test -e "{}/activate" \; -print 2>/dev/null
# Simply delete the folder
rm -rf ml-env

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 keras

Track dependencies:

# Save current packages
pip freeze > requirements.txt

# Install from requirements
pip install -r requirements.txt

Better: Use minimal requirements.txt

# requirements.txt - only specify what you actually use
torch>=2.0.0
transformers>=4.30.0
numpy>=1.24.0

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'

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 pytorch

Mamba uses a faster dependency solver than conda.

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.txt

Issue: “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/python

Issue: 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
# 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-env
# Search for compatible versions
conda search -c conda-forge tensorflow-gpu

# Install specific version
conda install -c conda-forge tensorflow-gpu=2.16.2

See Jupyter Notebooks for:

  • Adding environments as Jupyter kernels
  • Managing ipykernel
  • Jupyter-specific troubleshooting
# 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 envname
# 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 envname