20 uv: sharing and managing an environment
This chapter is adapted from the DSCI 521 notes, Virtual Environments: Python + uv. If you took DSCI 521, it is review.
Learning Objectives
- Recreate a collaborator’s environment exactly with
uv sync - Explain how
uv’s cache lets many projects share one download - Explain why
uv syncremoves packages that are not in the lockfile, and add packages withuv addinstead ofuv pip install - Inspect, remove, and upgrade packages, and separate development-only packages into a dependency group
20.2 Managing packages
Once a project is shared, you keep it up to date with a handful of commands.
20.2.1 Seeing what is installed
uv tree shows you the packages you asked for and what each of them dragged in:
Terminal
uv treeOutput
Resolved 6 packages in 4ms
penguin-analysis v0.1.0
├── palmerpenguins v0.1.6
│ ├── numpy v2.5.5
│ └── pandas v3.1.1
│ ├── numpy v2.5.5
│ └── python-dateutil v4.0.0
│ └── tzdata v2026.6
└── pandas v3.1.1 (*)
(*) Package tree already displayed
The indentation tells the story. We asked for palmerpenguins and pandas, and numpy, python-dateutil, and tzdata are along for the ride. This is the difference between pyproject.toml and uv.lock, drawn as a picture.
For a flat list of what is actually in the environment, uv pip list works the way you would expect:
Terminal
uv pip listOutput
Package Version
--------------- -------
numpy 2.5.5
palmerpenguins 0.1.6
pandas 3.1.1
python-dateutil 4.0.0
tzdata 2026.6
20.2.2 Removing a package
Terminal
uv remove pandasOutput
Resolved 6 packages in 9ms
Checked 5 packages in 0.74ms
This drops the line from pyproject.toml and updates uv.lock. Notice that nothing was uninstalled. palmerpenguins still needs pandas, so pandas stays in the environment as one of its dependencies. The tree above shows why.
Remove the package that needed it, and uv uninstalls everything that nothing else needs anymore:
Terminal
uv remove palmerpenguinsOutput
Resolved 1 package in 5ms
Uninstalled 5 packages in 215ms
- numpy==2.5.5
- palmerpenguins==0.1.6
- pandas==3.1.1
- python-dateutil==4.0.0
- tzdata==2026.6
One command keeps pyproject.toml, uv.lock, and .venv/ in step.
The rest of this chapter uses both packages, so add them back:
Terminal
uv add pandas palmerpenguinsOutput
Resolved 6 packages in 6ms
Installed 5 packages in 34ms
+ numpy==2.5.5
+ palmerpenguins==0.1.6
+ pandas==3.1.1
+ python-dateutil==4.0.0
+ tzdata==2026.6
20.2.3 Upgrading a package
Your lockfile pins exact versions on purpose, so uv sync will not silently upgrade anything. When you do want a newer version, ask for it:
Terminal
uv lock --upgrade-package pandasThis re-resolves just that package, leaving everything else pinned where it is, and writes the change into uv.lock. Then run uv sync to apply it.
Commit the resulting change to uv.lock. A diff on a lockfile is a record of exactly what changed in your environment and when, which is a useful thing to have when a project that worked last month stops working today.
20.2.4 Dependency groups
Some packages are needed to develop the project but not to run it, such as a test runner, a linter, or a formatter. These go in a dependency group rather than the main list:
Terminal
uv add --dev pytestpyproject.toml
[project]
name = "penguin-analysis"
version = "0.1.0"
requires-python = ">=3.14"
dependencies = [
"palmerpenguins>=0.1.6",
"pandas>=3.1.1",
]
[dependency-groups]
dev = [
"pytest>=9.2.0",
]uv sync installs the dev group by default, which is what you want on your own machine.
Groups are optional, and for a data analysis project you often do not need them. This book’s own pyproject.toml deliberately keeps everything in a single dependencies list so that there is exactly one install command to remember.
You will meet dependency groups again in the packaging chapters, where the distinction between “what my users need” and “what I need to develop this” starts to matter a great deal.