How to create and install Conda requirements.txt

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
4 min

banner

# How to create and install Conda requirements.txt

There are multiple ways used to create a Conda requirements.txt file.

If you need to be able to install the requirements.txt file in conda environments, use the conda list -e > requirements.txt command.

shell
conda list -e > requirements.txt

issue conda list e command

The conda list command lists the installed packages.

The -e or --explicit flag outputs the packages in a format that is suitable for a requirements.txt file.

shell
conda list -e

list packages in requirements txt format

The > bracket redirects the output of the command to a requirements.txt file.

shell
conda list -e > requirements.txt

# Installing the packages from the requirements.txt file

You can then use this requirements.txt file to install the specified packages.

shell
conda install --file requirements.txt

install packages from requirements txt

The --file flag enables you to specify a requirements.txt file when using the conda install command.

Make sure your terminal is located in the same directory as your requirements.txt file.

# Creating a Conda virtual environment from your requirements.txt file

You can also use the --file flag to create a conda virtual environment from your requirements.txt file.

shell
conda create --name <ENV_NAME> --file requirements.txt

create conda virtual environment from your requirements txt file

# The requirements.txt file from conda list is not suited for pip

Note that the requirements.txt file created from the conda list -e > requirements.txt command is not suited to be pip installed.

In fact, if you try to run the pip install -r requirements.txt command, you'd get an error.

shell
# ERROR: Invalid requirement: '_libgcc_mutex=0.1=main' (from line 4 of requirements.txt) # Hint: = is not a valid operator. Did you mean == ? pip install -r requirements.txt

requirements txt from conda not suitable for pip

# Creating a requirements.txt file that is suitable for pip install

If you need to create a requirements.txt file that is suitable for pip install -r requirements.txt:

  1. Make sure you have a conda environment active.
shell
# 1) If you need to create a Conda environment conda create --name my-env # 2) If you need to activate a Conda environment conda activate my-env
  1. Install pip in your conda environment.
shell
conda install pip

install pip in your conda environment

  1. Use pip to create your requirements.txt file.
shell
pip freeze > requirements.txt

use pip to create requirements txt file

NOTE: Open your requirements.txt file and check the formatting.

On some operating systems, you might get strange paths in your requirements.txt file.

If that is the case, issue the following command instead.

shell
pip list --format=freeze > requirements.txt

set format to freeze

Your requirements.txt file should be formatted as package-name==X.Y.Z.

For example:

requirements.txt
PySocks==1.7.1 python-dateutil==2.8.2 python-json-logger==2.0.7 PyYAML==6.0 pyzmq==25.0.0

Now you can use the requirements.txt file to install the modules inside a virtual environment.

  1. Create a virtual environment if you don't already have one.

Issue the command that corresponds to your Python installation and operating system.

shell
python -m venv venv # Or with python3 python3 -m venv venv # Or using py alias (Windows) py -m venv venv
  1. Activate the virtual environment.
shell
# activate on Unix or MacOS source venv/bin/activate # activate on Windows (cmd.exe) venv\Scripts\activate.bat # activate on Windows (PowerShell) venv\Scripts\Activate.ps1
  1. Install the modules from your requirements.txt file.
shell
pip install -r requirements.txt

If you get an error that contains specific package names, open your requirements.txt file and delete every line that is not formatted as package-name==X.Y.Z.

The format on all lines of your requirements.txt file should be package-name==VERSION.

If you continue to run into path errors:

  1. Make sure you have a conda environment active.
shell
# 1) If you need to create a Conda environment conda create --name my-env # 2) If you need to activate a Conda environment conda activate my-env
  1. Install pip in your conda environment.
shell
conda install pip

install pip in your conda environment

  1. Set the --format parameter to freeze when creating your requirements.txt file.
shell
pip list --format=freeze > requirements.txt

Then repeat the steps from before.

  1. Create a virtual environment if you don't already have one.

Issue the command that corresponds to your Python installation and operating system.

shell
python -m venv venv # Or with python3 python3 -m venv venv # Or using py alias (Windows) py -m venv venv
  1. Activate the virtual environment.
shell
# activate on Unix or MacOS source venv/bin/activate # activate on Windows (cmd.exe) venv\Scripts\activate.bat # activate on Windows (PowerShell) venv\Scripts\Activate.ps1
  1. Install the modules from your requirements.txt file.
shell
pip install -r requirements.txt

install from requirements txt created in conda env

When you are in a conda environment, you can also use the pip freeze and pip list --format freeze commands to see what output you get.

In my experience, the pip list --format freeze command produces legible requirements.txt files and pip freeze often produces erroneous output.

Here is an example of issuing pip freeze in a conda environment.

shell
pip freeze

issuing pip freeze in conda environment

Notice that the last package in the output is not formatted correctly.

All packages should be formatted as package-name==version-number.

If I run the pip list --format freeze command, everything works as expected.

issue pip list format freeze command

If you need to redirect the output to a requirements.txt file, use > requirements.txt.

shell
pip list --format=freeze > requirements.txt

set format to freeze

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.