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
conda env export
: Includes all dependencies with exact versionsconda env export --from-history
: Only includes explicitly installed packages
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
- Reproducibility: Exact environments across machines
- Collaboration: Easy environment sharing
- Version Control: Track environment changes in git
Additional Tips
- Use
--from-history
for cross-platform compatibility - Pin major versions when stability is crucial
- Separate development and production environments
- Use
mamba
for faster environment solving
This simple change has made our research much more reproducible!