Course contentsShow
← Data Science
Lesson 2043 of 2,14547. Reproducible ResearchFree lesson

Creating and Exporting Environment Specifications

How to generate requirements.txt or environment.yml files to document all installed packages and versions.

Creating and Exporting Environment Specifications

What you'll learn: How to capture your entire project environment into shareable files so others can perfectly replicate your setup.

Why Document Your Environment?

Imagine handing someone a recipe but forgetting to mention the oven temperature or ingredient brands. They might get close, but results will vary. The same happens in data science—your code might work perfectly on your machine but fail on a colleague's because they have different package versions.

Environment specification files solve this by recording exactly which packages and versions your project needs.

Creating Requirements Files (pip/venv)

When working with venv virtual environments, you create a requirements.txt file:

# Activate your environment first
source myenv/bin/activate  # or myenv\Scripts\activate on Windows

# Export all installed packages and their versions
pip freeze > requirements.txt

This creates a text file listing every package like pandas==1.5.3, numpy==1.24.2, etc. Anyone can recreate your environment with:

pip install -r requirements.txt

Creating Environment Files (conda)

For conda environments, you export to environment.yml:

# Export with exact versions
conda env export > environment.yml

# Or export only manually-installed packages (cleaner)
conda env export --from-history > environment.yml

Others recreate your environment with:

conda env create -f environment.yml

The .yml format includes the environment name, channels, and all dependencies—a complete blueprint.

Which Format to Use?

Use requirements.txt for pure Python projects with pip packages. Use environment.yml for conda-managed projects, especially those mixing Python with system libraries (like GDAL or scientific computing tools).

Key Takeaway: Export your environment specifications using pip freeze > requirements.txt for pip/venv projects or conda env export > environment.yml for conda projects—these files are your reproducibility insurance policy, letting anyone rebuild your exact setup.