Calmcode - uv: sync

Sync

1 2 3 4

There is a great tool called pip-tools that make it a lot easier to manage your pip dependencies. It can generate verbose requirements files and also sync your dependencies for you. We have a course for this tool here but it is also good to know that similar functionality can be found inside uv.

Suppose that you have a requirements.in file that looks like this:

scikit-learn==1.4.0

Then this file can be interpreted as the file with your dependencies that you plan om importing directly. But these dependencies might have dependencies themselves. You can generate a requirements.txt file from this requirements.in file by running:

uv pip compile requirements.in

This will print output that looks like this:

# This file was autogenerated by uv via the following command:
#    uv pip compile requirements.in
joblib==1.4.2
    # via scikit-learn
numpy==1.26.4
    # via
    #   scikit-learn
    #   scipy
scikit-learn==1.4.0
    # via -r requirements.in
scipy==1.14.1
    # via scikit-learn
threadpoolctl==3.5.0
    # via scikit-learn

You can see all the dependencies of the dependencies in this output and these can be written to a requirements.txt file.

Sync

From here you might be tempted to install these dependencies via:

uv pip install -r requirements.txt

This would work, but it might be a better habbit to resort to the sync command instead. This command will install the dependencies from the requirements.txt file but it will also remove any package that is not listed. That means that the requirements file is the source of truth at all times.

uv pip sync requirements.txt