Last updated: Apr 10, 2024
Reading timeยท3 min

Specify multiple -r options to pip install multiple requirements files, e.g.
pip install -r dev.txt -r prod.txt.
The -r option can be used multiple times to install the packages in multiple
requirements files.
dev.txt, test.txt and prod.txt files storing your requirements.# ๐๏ธ Writes the output of pip freeze to the specified file pip freeze > dev.txt pip freeze > test.txt pip freeze > prod.txt

You would use the following command to install the packages in the dev.txt and
test.txt files.
pip install -r dev.txt -r test.txt pip3 install -r dev.txt -r test.txt # ๐๏ธ If you don't have PATH environment variable set up correctly python -m pip install -r dev.txt -r test.txt python3 -m pip install -r dev.txt -r test.txt
The -r option is a
shorthand for --requirement and installs from the given requirements file.
pip command to install the packages in multiple requirements files.-r option inside a requirements.txt fileYou can also use the -r option inside of a requirements file.
For example, you might have common.txt, dev.txt and prod.txt requirements
files, where the common.txt file contains requirements for both dev and
prod.
This is the common.txt requirements file.
package_A=1.1.1 package_B=2.2.2
And here is dev.txt.
# ๐๏ธ Include packages in common -r common.txt package_C=3.3.3
And this is the prod.txt file that also includes the packages in common.txt.
# ๐๏ธ Include packages in common -r common.txt package_D=4.4.4
Now you would be able to pip install -r prod.txt and install the packages in
both prod.txt and common.txt in a single command.
However, this makes the management of packages more complicated than it needs to
be because you would have to handle the
pip freeze command in a way that
doesn't override your -r options.
A much simpler solution would be to remove the -r options in the files and
simply use multiple -r options directly in the pip install command.
pip install -r base.txt -r dev.txt pip3 install -r base.txt -r dev.txt
pipIf you run into issues when specifying multiple -r options,
upgrade your version of pip.
# ๐๏ธ If you have pip already installed pip install --upgrade pip # ๐๏ธ Or with pip3 pip3 install --upgrade pip # ๐๏ธ If you don't have pip in your PATH environment variable python -m pip install --upgrade pip python3 -m pip install --upgrade pip # ๐๏ธ On Windows py -m pip install --upgrade pip

After you upgrade pip, upgrade setuptools as well.
pip install --upgrade setuptools pip3 install --upgrade setuptools python -m pip install --upgrade setuptools python3 -m pip install --upgrade setuptools py -m pip install --upgrade setuptools

You can learn more about the related topics by checking out the following tutorials:
--no-cache-dir option