Django Friday Tips: Managing Dependencies

This one is not specific of django but it is very common during the development of any python project. Managing the contents of the requirements.txt file, that sometimes grows uncontrollably can be a mess. One of the root causes is the common work-flow of using virtualenv, install with pip all the required libraries and then do something like:

$pip freeze > requirements.txt

At the beginning this might work great, however soon you will need to change things and remove libraries. At this point, things start to get a little trickier, since you do not know which lines are a direct dependency of your project or if they were installed because a library you already removed needed them. This leads to some tedious work in order to maintain the dependency list clean.

To solve this problem we might use pip-tools, which will help you declare the dependencies in a simple way and automatically generate the final requirements.txt. As it is shown in the project readme, we can declare the following file:


Then we generate our “official” requirements.txt with the pip-compile command, that will product the following output:

# This file is autogenerated by pip-compile
# Make changes in, then run this to update:
#    pip-compile
amqp==1.4.8               # via kombu
anyjson==0.3.3            # via kombu
billiard==        # via celery
kombu==3.0.30             # via celery
pytz==2015.7              # via celery

Now you can keep track of where all those libraries came from. Need to add or remove packages? Just run pip-compile again.

By Gonçalo Valério

Software developer and owner of this blog. More in the "about" page.