Requirements.txt

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

The requirements.txt allows you to specify which packages and versions should be installed. Typically, the requirements.txt file is located in the root of the project. The requirements.txt file can be generated with pip freeze command. The dependencies from a requirements.txt file can be installed with pip install -r command.

Example

pyOpenSSL==0.13.1
pyparsing==2.0.1
python-dateutil==1.5
pytz==2013.7
scipy==0.13.0b1
six==1.4.1
virtualenv==16.3.0

Fine Tuning Requirements

https://pip.pypa.io/en/stable/reference/requirement-specifiers/
https://peps.python.org/pep-0440/
https://peps.python.org/pep-0508/

The versions can be specified with a >= operator, which say to install any version equal or newer than the specified version.

An upper bound can be specified with the < operator:

requests>=2.21.0, <3.0

The ~= syntax means compatible release (https://peps.python.org/pep-0440/#compatible-release), matching any candidate version that is expected to be compatible with the specified version.

Chaining Requirement Files

A requirements file can be "imported" in another requirements file:

requirements.txt:

# this is production
somepackage==1.0.0

requirements-dev.txt:

# this is development
-r requirements.txt
pytest>=4.2.0

How to Lock Down Direct and Transitive Dependencies

This can be done using pip-compile. When this is needed, process:

pip-compile

Generate requirements.txt with Poetry

poetry export