Using requirements or env.yml
This comes up often when moving between Python projects managed with pip and those managed with Conda. Let’s clarify:
requirements.txt
-
Standard in Python projects.
-
Used by pip.
-
Lists Python packages (and optionally versions).
-
Example:
numpy==1.26.0 pandas>=2.0.0 scikit-learn
Install with:
pip install -r requirements.txtenv.yml (or environment.yml)
-
Used by Conda.
-
YAML format describing the full environment:
- Python version
- Conda channels
- Conda packages
- Pip dependencies
-
Example:
name: myproject channels: - conda-forge - defaults dependencies: - python=3.11 - numpy=1.26 - pip - pip: - some_pip_only_package==0.3.1
Create environment from it:
conda env create -f env.ymlUpdate an existing environment:
conda env update -f env.yml --pruneExport an existing environment:
conda env export > env.ymlKey differences
requirements.txt→ lightweight, only lists pip packages.env.yml→ full specification of environment, includes Python version, Conda channels, and both Conda + pip packages.
When to use
-
Use
requirements.txtif:- Project is pip/venv-based.
- You want compatibility with tools like
pip installor deployment on services that expect pip.
-
Use
env.ymlif:- You want to reproduce a full Conda-managed environment (exact Python version, Conda dependencies).
- You work in data science projects where binary compatibility matters (e.g.,
numpy,pandas,pytorch).