Calmcode - pip-tools: constraints

Pip-compile with contrained requirements

1 2 3 4 5

The pipcompile command will respect constraints on project versions. So if you were to have this in your requirements.in file:

scikit-learn<1.3.0
django<2.2

Then you would get an output requirements.txt file with these contents:

#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
#    pip-compile --output-file=requirements.txt requirements.in
#
django==2.1.15
    # via -r requirements.in
joblib==1.4.0
    # via scikit-learn
numpy==1.26.4
    # via
    #   scikit-learn
    #   scipy
pytz==2024.1
    # via django
scikit-learn==1.2.2
    # via -r requirements.in
scipy==1.13.0
    # via scikit-learn
threadpoolctl==3.4.0
    # via scikit-learn

You can see what the version constraints are respected. For Django we see version 2.1.15 which is indeed below version 2.2 as we've specified.

Developer Experience

You can also have constraints across multiple input files. This is incredibly useful when you want to seperate your deployment packages from your testing packages. Consider this requirements-dev.in file.

-c requirements.txt
pytest==8.0.1
django-debug-toolbar<2.2

The top bit containts a -c requirements.txt file which adds constraints from our normal requirements. We can use this file to generate a requirements-dev.txt file, via:

pip-compile requirements-dev.in -o requirements-dev.txt

Now, our requirements-dev.txt will respect the constraints while also generating a requirements file that contains our base packages plus our testing tools.