Created on 18 Sep 2018 ;    Modified on 19 Sep 2018 ;    Translationitalian

Why in a Django app the items in install_requires of setup.py could be differ from the ones in requirements.txt of pip

This note was suggested to me reading this question on stackoverflow: Reference requirements.txt for the install_requires kwarg in setuptools setup.py file?

The context

To understand the context, I summarize it below.

When developing a Python application, you would share it with other developers. For this purpose Python provides a method of standardized distribution, using the distutils module, and the most advanced tools: setuptools.

Without going into details [1], one of the first steps is to compile a file, setup.py which describes how a Python module is formed, possibly how to compile it, and which libraries (i.e.: dependencies) it needs.

In setup.py the dependencies are indicated with the section:

...
install_requires = ['lib1', 'lib2', ...],
...

The Python installation software [2] processes the setup.py file performing the steps necessary for installation. When it processes the section install_requires, installs the libraries there listed.

On the other hand, we usually need to install an integer application environment. In this case we will launch a command like:

pip install -r requirements.txt

This command instructs pip to install all the modules listed in the file requirements.txt.

The question

In the aforesaid context, the author of the stackoverflow question asks:

"For what reason do I have to duplicate the list of dependencies, indicating it in both install_requires and in requirements.txt?"

The answer

The answers in stackoverflow emphasize that the needs the two aforementioned lists meet are different.

install_requires is intended to list the only required dependencies to the installation of the single module.

Instead requirements.txt lists all modules needed for installation of an application environment. And such an environment can require the installation of dozens of modules, each with its dependencies. Then usually requirements.txt is an overset of install_requires.

If the duplication of entries in the two lists annoys you, you can think to erase from requirements.txt the entries already present in install_requires, as long as they are necessary only for that module. Or that are correctly reported also in the setup.py of the other modules forming the application.

And what about Django?

Well: a Django app usually is developed in such a way to be reusable in various Django projects. So it has its own setup.py with the install_requires section.

On the other hand, when I install a Django project, I will use a series of apps, including the previous one. So tipically the file requirements.txt lists the apps, not the modules on which the apps depend. Unless the project has direct dependencies also from one or more modules used by the app(s). In this case I can have duplication of the declaration of a dependency.

Often the duplication of the declaration is not a problem. pip manage it by checking if a module is already installed. If so, it does not recharge it. Possibly declaring the version of the module already loaded and updating it if it is lower than the required level. The latter behavior could give us problems, in case different apps depend on the same module, but with different versions!


[1]An introduction, working in Django, can be found here: Advanced tutorial: How to write reusable apps; and in Python here: Tutorial on Packaging and Distributing Projects.
[2]

There is more than one. Here we consider pip, with the command:

pip install module_to_install