晴耕雨讀

Zonveld & Regenboek

TIL: Conda Environment Export and Import Best Practices

Posted at # TIL

The Problem

I was struggling to share my research environment with a colleague. Simply sharing requirements.txt wasn’t enough because we use conda for package management, and some packages aren’t available on PyPI.

The Solution

Export Environment with Exact Versions

# Export with exact versions (platform-specific)
conda env export > environment.yml

# Export cross-platform (recommended for sharing)
conda env export --from-history > environment.yml

Key Differences

Better Approach for Sharing

Create a minimal environment.yml:

name: research-env
channels:
  - conda-forge
  - bioconda
dependencies:
  - python=3.11
  - pandas
  - numpy
  - scikit-learn
  - jupyter
  - pip
  - pip:
      - seaborn

Create Environment from File

conda env create -f environment.yml

Why This Matters

Additional Tips

  1. Use --from-history for cross-platform compatibility
  2. Pin major versions when stability is crucial
  3. Separate development and production environments
  4. Use mamba for faster environment solving

This simple change has made our research much more reproducible!