Skip to main content

Dependencies (uv)

A little discussion about the nightmare of all developers...

To manage our dependencies, we use uv. uv replaces 3 different things:

  • pyenv for python version management;
  • pip for the installation of packages;
  • virtualenv for the isolation of the environment;
  • requirements.txt for the list of dependencies.

Concept​

uv is based on a file named pyproject.toml and a lock file named uv.lock. The pyproject.toml is used to list the dependencies needed for the project, and which versions of each package are required. Then, uv will take this config to find all the dependencies and sub-dependencies of each package, and try to find the latest version of each package which is compatible with all the other packages. Once this problem is solved, it will the list of all packages and their exact version into the lock file.

Add Dependencies​

info

You can search for packages on the Python Package Index.

Add a dependency (for example numpy):

uv add numpy~=1.20

Add a dev dependency (i.e. a package used for testing only):

uv add --dev <package_name>

Remove a dependency:

uv remove <package_name>
Semantic Versioning

Always indicate the version when adding a package to your project. By default, uv will use the latest version in the pyproject.toml, but will not prevent newer versions to be used. This is not a good practice, because newer versions may introduce breaking changes

To indicate the version, it's better to use the ~=[major].[minor] syntax (and not the ~=[major].[minor].[patch] syntax, with the 3rd number). This will ensure that the installed version is the latest that is still compatible with the specified one.

Update Dependencies​

When the last update was too old, and you need to update your packages, you have to update the lock file with the last versions to fix security issues for example. This is done automatically when you call uv run <...> after editing the pyproject.toml.

The following commands are for manually syncing the project:

uv lock --upgrade # lock with the newest compatible versions
uv lock # just lock and prefer already locked versions
uv sync --dev # lock + sync
warning

This command will only update the lock file, but not the pyproject.toml: if you set an old version of a package in your pyproject.toml, it will keep the old version to respect the pyproject.toml.

info

locking is the action of resolving dependencies version and writing the result in the lock file. syncing is the action of actually installing them in the project's environment.

See outdated dependencies​

Since the sync command does not update all the packages, we need a command to see the outdated packages. The output of the command will tell you which packages have a newer version.

uv tree --outdated

To upgrade a package, run uv upgrade <package>. This will save the new version in the pyproject.toml.

warning

Upgrading a major version introduce very often breaking changes. Make sure to review the upstream changelogs and intensively test the new version before merging the change.

Security issues​

To see all the security issues:

uv audit

Sometimes, one of your dependencies has a security issue. In this case, you need to update it as soon as possible to prevent any security issue in your project.

There are 3 cases:

  1. A patch has been released and you can update the package: you're fine, just do the update!
  2. A patch has been released but you have other dependencies that use this package and they did not update yet: wait a little bit that the parent package update its dependencies, and then see case n°1. If a package takes too much time to update its dependencies, then see n°3.
  3. No patch has been released: you'll have a lot of work to do, sorry 😢 You need to remove this dependency from your project, and try to find another one that can replace the package.