Skip to main content

21  uv: JupyterLab and standalone scripts

NoteAdapted from DSCI 521

This chapter is adapted from the DSCI 521 notes, Virtual Environments: Python + uv. If you took DSCI 521, it is review.

Learning Objectives

  1. Run JupyterLab inside a project’s uv environment, and check which kernel a notebook uses
  2. Declare a standalone script’s dependencies inside the script with uv add --script, and run it with uv run
  3. Summarize the uv commands and files that make a project reproducible

21.1 Working with JupyterLab

If you write notebooks, you need JupyterLab to see your project’s environment.

The simplest approach is to make JupyterLab part of the project:

Terminal
uv add jupyterlab ipykernel
uv run jupyter lab

ipykernel is the package that lets a Python environment show up as a kernel a notebook can attach to, and because JupyterLab is running inside the project environment, the default kernel is already the right one.

Tip

If you open a notebook and your imports fail, the first thing to check is which kernel the notebook is using (top-right corner in JupyterLab). A notebook attached to the wrong kernel is running against a different set of packages entirely, and the error you get, ModuleNotFoundError, looks identical to the package not being installed.

21.2 Standalone scripts

Not everything is a project. Sometimes you want a single .py file that you can email to someone and have it just work.

uv supports putting the dependencies inside the script.

First create a penguins2.py script:

penguins2.py
from palmerpenguins import load_penguins

penguins = load_penguins()
print(penguins.shape)

Then run:

Terminal
uv add --script penguins2.py palmerpenguins

which adds a comment block at the top of the file:

penguins2.py
# /// script
# requires-python = ">=3.14"
# dependencies = [
#     "palmerpenguins>=0.1.6",
# ]
# ///
from palmerpenguins import load_penguins

penguins = load_penguins()
print(penguins.shape)
ImportantThe script has to exist first

uv add --script edits a file that is already there. It does not create one. If you run it before you have written the script, you get:

Terminal
uv add --script penguins2.py palmerpenguins
Output
error: failed to read from file `penguins2.py`: No such file or directory (os error 2)

Write the script first, then add to it.

Anyone with uv installed can now run that file with no setup at all:

Terminal
uv run penguins2.py
Output
Installed 5 packages in 28ms
(344, 8)

uv reads the comment block, builds a throwaway environment, and runs the script in it. Those # lines are a comment to Python and a specification to uv, so the file is still a perfectly ordinary Python script.

TipNew since DSCI 521

The # /// script block is not specific to uv.

  • It is the inline script metadata format from PEP 723, which was accepted in January 2024.
  • PEP 723 replaced an earlier proposal, PEP 722, which would have listed a script’s packages one per line under a # Script Dependencies: comment. PEP 722 was rejected in October 2023 in favor of PEP 723’s TOML block, which can also say which Python the script needs.
  • As with pyproject.toml, the maintained version of the format lives in the Python Packaging User Guide, as the inline script metadata specification.

Any tool that follows the standard can read the block, not just uv.

This is a good option for a one-off analysis or a utility script. It is not a replacement for a project once you have more than one file.

21.3 Takeaway

uv ties one environment to one project directory, and uv run is how you use it. There is no environment to activate and no environment name to remember, which is much closer to how renv behaves in R than to how conda behaves.

The commands that do almost all the work:

Command What it does
uv init --bare start a project (creates pyproject.toml)
uv python pin 3.14 record the interpreter version
uv add <package> add a dependency, install it, update the lockfile
uv remove <package> the reverse
uv sync make the environment match uv.lock
uv run <command> run something inside the environment
uv tree see what you asked for and what came with it
uv lock --upgrade-package <package> deliberately move one package forward

And the three files that make the project reproducible, all of which belong in Git:

File Records
pyproject.toml what the project asks for
uv.lock what the project got, exactly, for every OS
.python-version which interpreter to use

.venv/ is not in that table. It is disposable, and uv sync rebuilds it in seconds.

This is the same division of labor you will see with R in renv: declaring packages in DESCRIPTION. DESCRIPTION and renv.lock play the parts of pyproject.toml and uv.lock. Learning to see that split, declared versus locked, is more durable than any particular tool’s command names.