Why is pip install -r requirements.txt not working in conda?

It is quite common to use requirements.txt to specify the python package requirements for the project. What is usually done is

pip install -r requirements.txt


## The Problem with Conda

When we create a new conda environment using

conda create --name my_project_env


pip is not installed by default. However, pip is usually installed on a user level, which means you could run the command

pip install -r requirements.txt


in this environment. But this will install all requirements to the python path globally instead of in the environment.

Check which pip

One could check the current pip using the following command

which pip


For example, one could get this result

/Users/itsme/anaconda3/bin/pip


If the above is the what you get, then it is likely that you are not using the pip in your environment.

Suppose you have an environment named amneumarkt, which pip should show something like the following.

/Users/itsme/anaconda3/envs/amneumarkt/bin/pip


## How to do it

To install requirements.txt in the environment, we have to use the pip installed within the environment. Thus we should install pip first by

conda install pip


Then we can install the requirements.txt.

Of course, there is a better way. We simply create a conda environment with pip installed in it:

conda create -n yourenv pip


Or if you want to specify your python version for this conda environment,

conda create -n python=3.7 yourenv pip


## Using environment.yml instead of requirements.txt

Refer to the documentation: Creating an environment from an environment.yml file

Planted: by ;

L Ma (2019). 'Installing requirements.txt in Conda Environments', Datumorphism, 03 April. Available at: https://datumorphism.leima.is/til/programming/python/python-anaconda-install-requirements/.