Managing package dependencies is crucial for reproducible Python environments. While pip uses requirements.txt files, Conda has its own environment specification formats but can also interact with pip. This often leads to questions about the best way to create and use requirement files within a Conda setup.
This guide explains how to generate both Conda-specific and pip-compatible requirement files from your Conda environment and how to install packages using them.
Conda vs. Pip Requirement Files: Key Differences
- Conda Explicit Specification (from
conda list -e): Contains detailed information including package names, exact versions, build strings, and often the channel they were installed from. It's designed for precisely replicating a Conda environment usingconda. It is not compatible withpip. - Pip
requirements.txt(frompip freezeorpip list --format=freeze): Typically lists package names and their versions using thepackage_name==versionformat. This is the standard recognized bypipand is more broadly compatible across different Python environment tools (likevenv).
Method 1: Creating a Conda-Specific Requirement File (conda list -e)
Use this method when your primary goal is to share or replicate an environment specifically for use with Conda.
Generating the File
Ensure your Conda environment is activated, then run:
conda activate your_env_name
conda list -e > requirements.txtconda list: Shows installed packages.-e(--explicit): Formats the output for recreation, including exact build specifications.> requirements.txt: Redirects the command's output into a file namedrequirements.txt. (Note: Naming itenvironment.ymland usingconda env export > environment.ymlis often the more standard Conda practice for environment sharing, but we'll stick torequirements.txtas requested.)
The resulting file will look something like this (content varies):
@EXPLICIT
https://repo.anaconda.com/pkgs/main/linux-64/bzip2-1.0.8-h7b6447c_0.conda#...
https://repo.anaconda.com/pkgs/main/linux-64/_libgcc_mutex-0.1-main.conda#...
https://repo.anaconda.com/pkgs/main/noarch/python-dateutil-2.8.2-pyhd3eb1b0_0.conda#...
...etc...Installing from the Conda File (conda install --file)
To install the packages listed in this Conda-specific file into an existing, active Conda environment:
conda activate target_env_name
conda install --file requirements.txtCreating an Environment from the Conda File (conda create --file)
To create a completely new Conda environment based on the specification:
conda create --name <ENV_NAME> --file requirements.txtImportant: Incompatibility with pip
This file format generated by conda list -e cannot be used with pip. Attempting pip install -r requirements.txt with this file will result in errors like:
ERROR: Invalid requirement: 'https://...' (from line ...)
Hint: It looks like a path. The path does not exist.or
ERROR: Invalid requirement: '_libgcc_mutex=0.1=main' (from line ...)
Hint: = is not a valid operator. Did you mean == ?Method 2: Creating a Pip-Compatible requirements.txt from Conda
Use this method when you need a standard requirements.txt file that can be used with pip, either within a Conda environment or a standard Python venv.
Ensure pip is Installed in Conda Environment
Make sure pip is available within your active Conda environment. If not, install it:
conda activate your_env_name
conda install pipGenerating with pip list --format=freeze (Recommended)
This command generally produces cleaner, standard output, especially within Conda environments. Ensure your source Conda environment is active.
conda activate your_env_name
pip list --format=freeze > requirements.txtGenerating with pip freeze (Alternative, Use with Caution)
The standard pip freeze command can also be used, but inside Conda environments, it sometimes includes extra details or path information that can cause issues when using the file elsewhere. Check the output file carefully if you use this method.
conda activate your_env_name
pip freeze > requirements.txtVerifying the Pip Format (package==version)
Open the generated requirements.txt. Its contents should look like this standard pip format:
packageA==1.2.3
packageB==4.5.6
another-package==0.1.0
...etc...Remove any lines that don't follow this format (e.g., lines starting with #, -e, or paths, unless intended).
Installing with pip install -r (in Conda or venv)
This pip-compatible file can now be used to install packages in any environment (Conda or standard venv) where pip is available.
- Activate the target environment:
conda activate target_env_name python3 -m venv new_venv source new_venv/bin/activate # Linux/macOS new_venv\Scripts\activate.bat # Windows CMD new_venv\Scripts\Activate.ps1 # Windows PowerShell - Install from the requirements file:
pip install -r requirements.txt
Choosing Which Format to Create
- Use
conda list -e > requirements.txt(or better,conda env export > environment.yml) when you want to precisely replicate a Conda environment for another Conda user, including specific builds and channels. Install usingconda install --fileorconda create --file. - Use
pip list --format=freeze > requirements.txtwhen you need a standard, pip-compatible file for broader compatibility, sharing with non-Conda users, or using with tools that expect the standardrequirements.txtformat. Install usingpip install -r.
Conclusion
You can generate requirement files from your active Conda environment for different purposes:
- For Conda-specific replication, use
conda list -e > requirements.txtand install withconda install --fileorconda create --file. - For a standard, pip-compatible file, ensure
pipis installed in your Conda environment and usepip list --format=freeze > requirements.txt(preferred) orpip freeze > requirements.txt. Install usingpip install -r.
Understanding the distinction between these formats and their corresponding installation commands is key to effectively managing dependencies within and outside of Conda environments. Using virtual environments (either Conda environments or standard venv) is always recommended for isolating project dependencies.