Using Virtual Environments
What you'll learn: How to use venv and conda to create isolated Python environments that prevent dependency conflicts across multiple ML projects.
Why Virtual Environments Are Essential
Imagine working on three ML projects: one requires TensorFlow 2.10 with Python 3.8, another needs PyTorch 2.0 with Python 3.10, and a third uses an older scikit-learn version. Installing all these dependencies globally would create chaos—version conflicts, broken imports, and hours of debugging.
Virtual environments solve this by creating isolated "sandboxes" where each project has its own Python interpreter and dependencies, completely separate from others.
Two Main Tools: venv and conda
venv (built into Python) creates lightweight environments:
# Create a new environment
python -m venv ml_project_env
# Activate it (Linux/Mac)
source ml_project_env/bin/activate
# Activate it (Windows)
ml_project_env\Scripts\activate
# Now install dependencies
pip install torch torchvision
# Deactivate when done
deactivate
conda (from Anaconda/Miniconda) offers more power, managing both Python versions and non-Python dependencies:
# Create environment with specific Python version
conda create -n ml_project python=3.10
# Activate
conda activate ml_project
# Install packages
conda install pytorch torchvision -c pytorch
# Deactivate
conda deactivate
Which Should You Use?
venv: Simpler, faster, Python-standard. Use when you only need Python packages and don't need to switch Python versions.conda: More powerful. Essential when you need specific Python versions, CUDA libraries, or complex scientific packages with C dependencies.
Both work seamlessly with the requirements.txt files you learned previously—just activate your environment first, then pip install -r requirements.txt.
Key Takeaway: Virtual environments are non-negotiable for reproducible ML work. Use venv for simplicity or conda for full control over Python versions and system-level dependencies—either way, never install ML libraries globally.