85

To create Python virtual environments I use virtualenv and pip. The workflow is very simple:

$ virtualenv project
$ cd project
$ . bin/activate
$ pip install -r /path/to/requirements/req1.txt
$ pip install -r /path/to/requirements/req2.txt

The number of different requirement files can grow enough to make handy to have a way to include them at once, so I'd rather prefer to say:

$ pip install -r /path/to/requirements/req1_req2.txt

with req1_req2.txt containing something like:

include /path/to/requirements/req1.txt
include /path/to/requirements/req2.txt

or otherwise:

$ pip install -r /path/to/requirements/*.txt

None of that works and however much simple it could be, I can't figure out how to do what I want.

Any suggestion?

Paolo
  • 20,112
  • 21
  • 72
  • 113
  • What about script which accept input like req1|req2... split it and then call os.system with proper pip command ? – mrok Jul 28 '12 at 20:14

2 Answers2

143

The -r flag isn't restricted to command-line use only, it can also be used inside requirements files. So running pip install -r req-1-and-2.txt when req-1-and-2.txt contains this:

-r req-1.txt
-r req-2.txt

will install everything specified in req-1.txt and req-2.txt.

Felix Loether
  • 6,010
  • 2
  • 32
  • 23
  • Can this be used to extend and overwrite? E.g. req-1.txt specifies `foo==1.3` and req-2.txt `foo==1.4`, would it install 1.4? – djangonaut Oct 20 '16 at 10:04
  • 7
    Answering my own question: No, it doesn't work: `Double requirement given: foo==1.4 ...` – djangonaut Oct 20 '16 at 10:10
  • 3
    @beluga.me it works IF your pip has the fix. If you have pip 1.5.X which is default for say Debian wheezy or jessie, than doing *pip install -U pip* (or similar) should fix this problem. – Sergey Mar 27 '17 at 20:11
8

Just on a note, you can also split the requirements based on your groupings and embed them in a single file ( or again can prepare multiple requirements file based on your environment), that you can execute.

For example, the test requirements here:

requirements-test.txt

pylint==2.4.4
pytest==5.3.2

The dev requirements here:

requirements-dev.txt

boto3>=1.12.11

Master requirements file containing your other requirements:

requirements.txt

-r requirements-dev.txt
-r requirements-test.txt

Now, you can just install the requirements file embedding your other requirements

pip3 install -r requirements.txt
TechFree
  • 2,600
  • 1
  • 17
  • 18