Pip install multiple requirements files in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Pip install multiple requirements files in Python

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.

The example assumes that you have dev.txt, test.txt and prod.txt files storing your requirements.
shell
# ๐Ÿ‘‡๏ธ Writes the output of pip freeze to the specified file pip freeze > dev.txt pip freeze > test.txt pip freeze > prod.txt

pip install multiple requirements files

You would use the following command to install the packages in the dev.txt and test.txt files.

shell
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.

The option can be used multiple times in the same pip command to install the packages in multiple requirements files.

# Using the -r option inside a requirements.txt file

You 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.

common.txt
package_A=1.1.1 package_B=2.2.2

And here is dev.txt.

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.

prod.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.

shell
pip install -r base.txt -r dev.txt pip3 install -r base.txt -r dev.txt

# Upgrade your version of pip

If you run into issues when specifying multiple -r options, upgrade your version of pip.

shell
# ๐Ÿ‘‡๏ธ 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

upgrade pip version

After you upgrade pip, upgrade setuptools as well.

shell
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

upgrade setuptools

# 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.

Copyright ยฉ 2024 Borislav Hadzhiev