The case for a min-requirements.txt file

2017-01-03

Most projects that I know use just a requirements.txt file, which is the product of running:

1
$ pip freeze > requirements.txt

This is a file in the format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
blinker==1.4
docutils==0.13.1
feedgenerator==1.9
Jinja2==2.8
MarkupSafe==0.23
pelican==3.7.0
Pygments==2.1.3
python-dateutil==2.6.0
pytz==2016.10
six==1.10.0
Unidecode==0.4.19

It specifies all the dependencies of a project, to allow easy restoration of the environment, and in a strict-version format to stop automatic updates. This makes sense, since every update brings the potential for bugs (developers can make backwards-incompatible changes, on purpose or not). Then, you can restore the state of the working environment of a project by running (usually in a virtual environment):

1
$ pip install -r requirements.txt

This creates a problem when you want to remove a dependency. Sometimes, you'll find that a feature you need is better implemented in a different dependency than the one you are currently using and you may want to change. How do you define, from that list, which are the dependencies of the dependency that you want to remove?

You need to look at the code of the dependency, to see which dependencies it has, and then remove them from the requirements.txt file. This is manual and error-prone.

A solution that I think works well enough is having a min-requirements.txt file alongside the requirements.txt file. You put in this file only the minimal dependencies of your project (in the strict-version format, since it's a good idea to keep things from updating automatically) and when you need to remove one of them, you just need to remove that line and run:

1
$ pip install -r min-requirements.txt

All the minimal dependencies and their dependencies will be installed. The only downside is that you'll need to restore the version of any sub-dependency that got updated from the previous min-requirements.txt , but still, that's easier than looking for the dependencies of a dependency in its source code.

The above requirements.txt would become min-requirements.txt as:

1
pelican==3.7.0