52

How can I do define multiple requirements files in my requirements.txt file.

-r base.txt
-r test.txt

The current behavior is that pip only installs packages from test.txt. I'd expect pip to install packages found in both base.txt and test.txt. I could have sworn I've seen someone do this in the past, but I can't find any examples.

1
  • Are you sure you have the latest pip? pip install -U pip Commented Apr 20, 2016 at 12:54

4 Answers 4

85

pip accepts multiple -r arguments:

pip install -r reqs1.txt -r reqs2.txt

The help for pip install says:

-r, --requirement
Install from the given requirements file. This option can be used multiple times.

4
  • 2
    The OP seems to be using -r multiple times but it doesn't work. Do you know what the minimum version of pip is for it to work? Commented Apr 20, 2016 at 12:50
  • @JohnZwinck I was using pip 8.1.1 and python 2.7.8. I'll see if I have an older pip lying around. Commented Apr 20, 2016 at 12:55
  • @JohnZwinck I tried using pip 7.1.2 and encountered permissions errors because pip was trying to copy files from the system python's site-packges into my virtualenv. I guess pip's caching was different in 7.1.2. I couldn't see anything on pip's guthub issues searching for "multiple requirements"; searching for just "requirements" brings up 20 pages of issues :( So I can't say precisely what version, but at least > 7.1.2. Commented Apr 20, 2016 at 13:04
  • Is there any performance gain when we add multiple -r arguments vs run pip install sequentially on a set of requirement files?
    – harry1102
    Commented Sep 28, 2022 at 15:10
40

You can have one file "include" the other; for example, if you put this in file2.txt:

-r file1.txt
Django
Flask
etc.

Then when you do pip install -r file2.txt, it will also install things from file1.txt.

I often use this strategy to have a "base" requirements file, and then only specify those things that are required at each stage (development, testing, staging, production, etc.)

2
  • How can I accomplish the same thing when I'm installing a locally-created package through its setup.py? e.g. pip install -e my-package. When I have multiple requirements files I get this error: "error in my-package setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Invalid requirement, parse error at "'-r requi'".
    – DataMan
    Commented Mar 13, 2019 at 13:42
  • 1
    How do you handle pip freeze with this?
    – Sameera K
    Commented Jan 14, 2022 at 4:43
9

I have many requirements in different directories and solve this problem as:

sudo find . -name "requirement*" -type f -exec pip3 install -r '{}' ';'
0

You can install the contents of all available requirements.txt files in this directory in a single "pip install" shot, as long as there are no spaces in the directory paths:

find -name requirements.txt -printf '--requirement %p ' | 
  xargs --no-run-if-empty pip install

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.