You can install pip-tools
via:
python -m pip install pip-tools
Once that is done, you should be able to confirm that it is installed via:
python -m piptools
We're going to focus on the compile
command first, which you can run via pip-compile
or python -m piptools compile
.
How pip-compile
works
The idea behind pip-compile
is that we are going to generate a requirements.txt
file from a requirements.in
file. The requirements.in
file will be the entry point for the developer and the requirements.txt
file will then become a very exact representation of all the packages that your project needs.
If you have a requirements.in
file that looks like this:
scikit-learn==1.4.2
Then you can run this command:
pip-compile requirements.in -o requirements.txt
To generate a new requirements file that looks like this:
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=requirements.txt requirements.in
#
joblib==1.4.0
# via scikit-learn
numpy==1.26.4
# via
# scikit-learn
# scipy
scikit-learn==1.4.2
# via -r requirements.in
scipy==1.13.0
# via scikit-learn
threadpoolctl==3.4.0
# via scikit-learn
Notice how all the dependencies are listed here with exact version numbers? Pip-tools is making sure these version numbers are up to the specification of the input file. This is great!