How to Create and Install requirements.txt
in Conda & Pip Formats in Python
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 freeze
orpip list --format=freeze
): Typically lists package names and their versions using thepackage_name==version
format. This is the standard recognized bypip
and 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:
# Activate your source environment first (if not already active)
conda activate your_env_name
# Generate the explicit specification file
conda list -e > requirements.txt
conda 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.yml
and usingconda env export > environment.yml
is often the more standard Conda practice for environment sharing, but we'll stick torequirements.txt
as requested.)
The resulting file will look something like this (content varies):
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: linux-64
@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:
# Make sure the target Conda environment is active
conda activate target_env_name
# Install packages from the file
conda install --file requirements.txt
Creating an Environment from the Conda File (conda create --file
)
To create a completely new Conda environment based on the specification:
# Replace <ENV_NAME> with your desired new environment name
conda create --name <ENV_NAME> --file requirements.txt
Important: 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:
# Activate your source environment first
conda activate your_env_name
conda install pip
Generating with pip list --format=freeze
(Recommended)
This command generally produces cleaner, standard output, especially within Conda environments. Ensure your source Conda environment is active.
# Activate your source environment first
conda activate your_env_name
# Generate pip-compatible file
pip list --format=freeze > requirements.txt
Generating 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.
# Activate your source environment first
conda activate your_env_name
# Generate using pip freeze (check output file afterwards)
pip freeze > requirements.txt
Verifying 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:
# Example: Activate a Conda environment
conda activate target_env_name
# OR Example: Create and activate a standard Python venv
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 --file
orconda create --file
. - Use
pip list --format=freeze > requirements.txt
when you need a standard, pip-compatible file for broader compatibility, sharing with non-Conda users, or using with tools that expect the standardrequirements.txt
format. 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.txt
and install withconda install --file
orconda create --file
. - For a standard, pip-compatible file, ensure
pip
is 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.