Why Virtual Environments?
Without environments, all packages install globally, leading to version conflicts:
- Project A → TensorFlow 2.12
- Project B → TensorFlow 1.15
A virtual environment ensures isolated dependencies per project.
Setting Up Conda
Install Miniconda (lightweight version of Anaconda):
# Linux/Mac wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh # Windows (PowerShell) Invoke-WebRequest -Uri https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe -OutFile Miniconda3.exe Start-Process Miniconda3.exe
Creating & Managing Environments
# Create environment with Python 3.9 conda create -n ml-env python=3.9 # Activate environment conda activate ml-env # Deactivate conda deactivate
Installing Packages
# Install from conda conda install numpy pandas scikit-learn # Install from pip inside conda pip install xgboost lightgbm
Exporting & Sharing Environments
Reproducibility requires sharing your environment setup.
# Export conda env export > environment.yml # Recreate elsewhere conda env create -f environment.yml
Example environment.yml:
name: ml-env
dependencies:
  - python=3.9
  - numpy
  - pandas
  - scikit-learn
  - pip
  - pip:
      - xgboost
      - lightgbm
Best Practices
- One environment per project.
- Pin versions (e.g., numpy=1.24.3) for stability.
- Use conda env export before deployment.