Creating Reproducible Python Environments with Anaconda's environment.yml
Learn how to define a reproducible Python environment with Anaconda's environment.yml file, see a worked example, understand its limits, and avoid common pitfalls.
29 Apr 2026, 19:38 UTC

Useful answer
You can recreate a Python environment exactly by declaring its packages, versions, and channels in an environment.yml file and running a single conda env create command. This gives you a portable, one‑click way to share a working setup with teammates or CI systems.
How the mechanism works – a worked example
The environment.yml file tells conda’s solver which channels to search and which exact package builds to install. The solver fetches packages in channel order, creates an isolated environment, and records the final spec so you can later audit or reproduce it.
Example environment.yml
# environment.yml
name: my_project
channels:
- conda-forge
- defaults
dependencies:
- python=3.11
- numpy=1.26.4=py31h8a188b0_0 # exact build for linux‑64
- pandas=2.2.0
- pip
- pip:
- requests==2.31.0
- flask==3.0.0
Explanation of the sections:
name:– the environment identifier you will use withconda activate.channels:– list of package repositories; conda searches them in order, soconda-forgeis checked beforedefaults.dependencies:– packages installed via conda. Pinning to an exact build string (=py31h8a188b0_0) forces that specific binary.pip:sub‑section – isolates pip packages from the conda solver, reducing the chance of duplicated or conflicting installations.
Creating the environment
Run the following in a terminal (no special permissions required; you just need conda on your PATH):
# 1. Create a temporary directory to isolate the test
mkdir -p /tmp/env-demo && cd /tmp/env-demo
# 2. Save the example above as environment.yml
# 3. Create the environment
conda env create -n demo_env -f environment.yml
# 4. Activate and verify
conda activate demo_env
conda list | grep -E 'numpy|pandas|python'
Expected checks:
- The output should show
numpy 1.26.4 py31h8a188b0_0(or the exact build you pinned). pandasandpythonshould match the versions you specified.- Pip packages appear at the bottom of
conda listunder thepipsection.
Risks:
- If a pinned build string does not exist for your platform (e.g., you copied a Linux build string to Windows), conda will fail to solve the environment.
- Over‑constraining versions can cause the solver to report conflicts, especially when transitive dependencies require a different range.
Limits of the approach
While environment.yml gives strong reproducibility, it has boundaries you should be aware of:
- Channel availability: A version pin only works if the exact build is present in one of the listed channels. If the package is missing, conda will either fall back to a different build (potentially breaking ABI compatibility) or abort with an unsatisfiable environment error.
- Platform‑specific builds: Build strings encode the operating system and compiler used. Pinning to a Linux‑specific build will not work on macOS or Windows; you need either platform‑agnostic pins (e.g.,
numpy=1.26.4) or separate files per OS. - System‑level dependencies: Drivers, CUDA toolkit, or external libraries (e.g.,
ffmpeg) are not captured by conda and must be installed separately. - Solver performance: Very large numbers of pinned packages can slow down environment creation because the solver has fewer degrees of freedom.
Common mistakes and how to avoid them
- Omitting the
channels:list. Without it, conda uses only thedefaultschannel, which may lack newer builds fromconda-forge. Always declare the channels you intend to use. - Mixing
condaandpippackages in the same top‑level list. This can lead to duplicated installations or version conflicts. Use thepip:subsection underdependenciesto keep pip packages isolated. - Forgetting to run
conda env updateafter editing the file. The existing environment stays unchanged until you explicitly update it. After a change, run:
conda env update -n demo_env -f environment.yml --prune
The --prune flag removes packages that are no longer listed, keeping the environment in sync.
numpy>=1.26.0,<1.27.0) and let conda pick the appropriate build for each platform.Practical verification steps
- Create a temporary directory and write a minimal
environment.yml(you can use the example above). - Execute
conda env create -n verify_env -f environment.yml. - Activate the environment and run
conda list. Confirm that the pinned packages appear with the exact versions/builds you specified. - Modify a version in the file (e.g., change
numpy=1.26.4tonumpy=1.26.3) and runconda env update -n verify_env -f environment.yml --prune. Verify thatconda listnow shows the new version. - To test a missing channel, temporarily remove
conda-forgefrom the list and attempt creation; observe the solver error or fallback behavior.
These steps let you see both the success case and the limits described above without affecting any production environments.
Rollback
Creating or updating an environment changes system state (it writes files into $CONDA_PREFIX/envs). To undo, simply remove the environment:
conda env remove -n verify_env
This deletes the environment directory and all its packages, returning the disk space to the system.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.