Manage Python Dependencies with uv¶
uv is a fast Python package and project manager that consolidates pip,
virtualenv, and pip-tools into a single tool. This guide covers installing
uv locally and on the cluster, managing project dependencies, running scripts
reproducibly, and submitting Slurm batch jobs.
Before you begin¶
-
Obtain a Mila account, enable cluster access and MFA, install
uvandmilatools, configure SSH access and connect to the cluster for the first time.
What this guide covers¶
- Install
uvon a local machine and on the Mila cluster - Create a project and declare dependencies in
pyproject.toml - Add, update, and remove packages
- Understand how
uvresolves dependencies safely - Reproduce the environment on a new node using
uv.lock - Run standalone scripts with inline dependencies
- Submit a Slurm job that runs your script with
uv run python - Install CLI tools system-wide with
uv tool
Install uv¶
On a local machine¶
Run the following command in a terminal:
Verify the installation:
If uv is not found
If uv --version returns command not found, close and reopen your
terminal to apply the updated PATH.
On the cluster¶
Connect to a login node:
Then run the install command:
If uv is not found
If uv --version returns command not found, exit the SSH session and
reconnect to apply the updated PATH.
Create a project¶
Initialize a new project with a pinned Python version:
The command creates the following files:
The virtual environment (.venv/) and lockfile (uv.lock) are created
the first time uv add or uv run is called.
Initialize a project in an existing directory
Run uv init without a directory name from inside an existing directory to
initialize a project in place.
Manage dependencies¶
Add packages¶
uv add installs a package, updates pyproject.toml, and synchronizes
the virtual environment in a single step:
Specify version constraints using PEP 440 syntax:
How uv resolves dependencies¶
pip installs packages one command at a time and only checks constraints for
the packages in that command β it can silently break packages that are already
installed:
"when you run a
pipinstall command,piponly considers the packages you are installing in that command, and may break already-installed packages." βpipdocumentation
uv add resolves the entire dependency graph before writing anything to
disk. If adding a new package would conflict with an existing one, uv reports
the conflict and aborts rather than leaving the environment in a broken state.
Add development dependencies¶
Development dependencies β testing tools, linters, formatters β are only needed
during local development, not when running your research code. Add them with
--dev:
Remove packages¶
Reproduce the environment¶
Why pin exact versions¶
Without a lockfile, two installs of the same pyproject.toml can produce
different package versions β a transitive dependency may have released a new
patch between installs. This can silently change model behaviour or break code
across nodes and collaborators.
uv.lock records the exact resolved version of every dependency, direct and
transitive. Committing this file to version control guarantees that uv sync
installs a bit-for-bit identical environment on any machine.
Sync the environment¶
Free disk space by clearing the cache
If your home directory is running low on space, first remove stale entries with:
This removes outdated entries while keeping packages still used by your projects. If you need to reclaim more space, remove the entire cache with:
uv will re-download packages automatically the next time they are needed.
Run code¶
Within a project¶
uv run executes a command inside the project environment. Before running, uv
checks whether the lockfile matches pyproject.toml and updates the virtual
environment if needed. No manual activation of the virtual environment is
required.
Run a tool registered in the environment:
Run a Python script with the project environment loaded:
Use uv in a Slurm job¶
Declare the training script's dependencies in pyproject.toml using uv add:
| job.sh | |
|---|---|
Submit the job from the project root directory:
uv run syncs the environment against uv.lock before executing train.py, so
the job always uses the pinned dependencies regardless of which compute node
runs it.
Install CLI tools with uv tool¶
uv tool install installs a package's CLI executables into a persistent,
isolated environment and adds them to PATH. Use this for command-line tools
needed across projects β separate from uv add, which adds packages imported in
project code.
Install a tool¶
Install wandb to access the Weights & Biases CLI:
The wandb command is now available system-wide.
List installed tools¶
Upgrade tools¶
Upgrade a single tool:
Upgrade all installed tools at once:
Uninstall a tool¶
Standalone scripts with inline dependencies¶
For one-off scripts or utilities that do not belong to a project, PEP 723 metadata lets you declare dependencies directly inside the script file.
Use uv add --script to add dependencies to the script. uv will write the #
/// script metadata block automatically:
The resulting script looks like:
| experiment.py | |
|---|---|
Run the script with:
uv reads the metadata block, creates a temporary isolated environment with
those exact dependencies, runs the script, and discards the environment. The
project's pyproject.toml is not modified.
Key concepts¶
pyproject.toml- The project configuration file. Lists runtime and development dependencies,
the required Python version, and project metadata. Updated automatically by
uv addanduv remove. uv.lock- A lockfile generated by
uvthat records the exact resolved version of every dependency. Commit this file to version control to guarantee reproducible environments across nodes and collaborators. uv run- Executes a command inside the project's virtual environment. Use
uv run python <file>to run a project script. Pass--script <file>only for standalone scripts with PEP 723 inline metadata. Syncs the environment againstuv.lockbefore running, making manual activation unnecessary. - virtual environment (
.venv/) - An isolated directory containing the Python interpreter and installed packages
for the project. Created and managed automatically by
uv. - dependency group
- A named set of dependencies in
pyproject.toml. The default development group (--dev) holds tools likepytestthat are only needed during local development, not when running your research code on the cluster. uv tool install- Installs a CLI tool into a persistent isolated environment and adds its
executables to PATH. Use for command-line tools needed across projects
(e.g.
wandb,mila). For packages imported in code, useuv addinstead. - tool environment
- A persistent isolated virtual environment created by
uv tool install, stored separately from any project's.venv. Removed withuv tool uninstall. - PEP 723 script metadata
- A
# /// scriptblock at the top of a.pyfile that declares the script's Python version and dependencies. Used for one-off scripts that do not belong to a project.uv add --scriptwrites this block automatically;uv run --scriptreads it and creates a temporary isolated environment, leavingpyproject.tomlunchanged.