Sub topic 8
Environments, uv sync, and uv run
This chapter assumes you have read the earlier chapters on pyproject.toml, dependencies, and uv.lock. You should know what a virtual environment is and how uv add changes the [project] table. Here you will learn how uv turns a lockfile into an installed environment, and when to use uv sync or uv run.
The project environment
uv keeps one virtual environment per project, in a .venv directory next to pyproject.toml. You rarely create it by hand. uv sync, uv run, and uv add create it when it is missing. If the existing environment does not satisfy requires-python, uv recreates it.
The environment is a cache of the lockfile. It is disposable, it belongs in .gitignore, and you can delete it and rebuild it at any time. The files you commit are pyproject.toml and uv.lock.
rm -rf .venv
uv syncThe second command recreates .venv and installs exactly what uv.lock records. To put the environment somewhere else, set UV_PROJECT_ENVIRONMENT to a path.
What uv sync does
uv sync makes the environment match the lockfile. It runs three steps:
- It checks whether
uv.lockis up to date withpyproject.toml. If not, it re-resolves and rewrites the lockfile. - It creates or reuses
.venv. - It installs, upgrades, and removes packages until the environment matches the lock.
By default the sync is exact. Packages that are in the environment but not in the lockfile are uninstalled. That includes anything you added with uv pip install. Pass --inexact to keep extra packages.
uv sync also installs your own project into the environment, in editable mode, when the project is a package. Editable means imports point at your source files, so edits take effect without reinstalling.
The following diagram shows how the files relate.
Choosing what to install
By default, uv sync installs the runtime dependencies from [project] dependencies and the dev dependency group. Flags change that selection:
| Flag | Effect |
|---|---|
| `--no-dev` | Skip the `dev` group. |
| `--group NAME` | Also install the named dependency group. |
| `--only-group NAME` | Install only that group, not the project's own dependencies. |
| `--all-groups` | Install every dependency group. |
| `--extra NAME` | Install an optional dependency set from `[project.optional-dependencies]`. |
| `--all-extras` | Install every extra. |
| `--no-install-project` | Install dependencies but not the project itself. |
| `--no-editable` | Install the project as a regular, non-editable package. |
--no-install-project is useful in Docker builds. You can install dependencies in an early layer that is cached, and copy the source code in afterwards.
uv sync --no-dev --no-install-projectWhat uv run does
uv run runs a command inside the project environment. Before it starts the command, it does the lock and sync steps described above. If you edit pyproject.toml and run uv run pytest, uv updates uv.lock and the environment first. You never need to activate .venv.
uv run pytest
uv run python -m weather_cli
uv run weather
uv run -- python -c "import sys; print(sys.prefix)"Use -- to separate uv's options from the command's own options when they could be confused.
Unlike uv sync, uv run syncs inexactly by default. It leaves extra packages in place, so a package you installed by hand is not removed. If you want strictness, run uv sync yourself, or use uv run --exact.
Some options adjust the run:
--with PACKAGEadds a package for this one run without changingpyproject.toml. For example,uv run --with rich python demo.py.--python 3.11runs with a different interpreter. uv builds a separate environment for it if needed.--group NAMEand--no-devselect dependency groups, as withuv sync.--no-projectignores the project in the current directory.--isolatedruns in a throwaway environment that does not touch.venv.
Controlling lock and sync checks
Three options control how uv run and uv sync treat the lockfile.
| Option | Behavior |
|---|---|
| (default) | Update `uv.lock` if it is out of date, then sync. |
| `--locked` | Fail with an error if `uv.lock` is out of date. Never write the lockfile. |
| `--frozen` | Use `uv.lock` as it is, without checking it against `pyproject.toml`. |
| `--no-sync` | With `uv run`, skip the sync and use the environment as it is. |
--locked is a check: "the lockfile must already agree with pyproject.toml." --frozen is a trust: "install what the lockfile says and do not look at pyproject.toml." The CI chapter returns to these. For now, remember that the default modifies uv.lock silently, and the other modes do not.
A worked session
This session uses the weather-cli project from earlier chapters.
uv sync
uv run python -c "import httpx; print(httpx.__version__)"
uv add rich
uv run --no-sync python -c "import rich; print('rich ok')"
uv sync --no-dev
uv run --no-sync pytestStep by step:
uv synccreates.venvand installshttpx,typer,pytest, and the project.uv runprints the installedhttpxversion.uv add richeditspyproject.toml, updatesuv.lock, and installsrich.uv run --no-syncuses the environment as it is. It works here becauseuv addhas already installedrich.uv sync --no-devremovespytest, because sync is exact and the dev group is now excluded.- The last command fails with a "program not found" error for
pytest, since--no-syncstops uv from reinstalling it. Runninguv run pytestwithout--no-syncwould put it back.
Common errors and gotchas
- A stale
VIRTUAL_ENVvariable. If another environment is activated, uv warns thatVIRTUAL_ENVdoes not match the project environment. It ignores the active one and uses.venv. Deactivate the other environment, or pass--activeto use it deliberately. - Running tools directly. Typing
pytestin a shell without activation runs a globalpytest, or none at all. Useuv run pytest, or activate.venvyourself. - Using
uv pip installin the project environment. The nextuv syncremoves these packages. Add them withuv add, or useuv sync --inexact. - Wrong interpreter. If
requires-pythonor.python-versionchanges, uv rebuilds.venv. Anything you installed manually is lost. - Committing
.venv. Do not. Environments contain absolute paths and platform-specific binaries. - Editing
uv.lockby hand. It is generated. Changepyproject.tomland let uv re-lock.
Exercises
Exercise
You run uv pip install requests in a project, and later run uv sync. Is requests still installed? Why, and how would you keep it?
Show answer
No. uv sync is exact by default and removes packages that are not in uv.lock. To keep it, add it properly with uv add requests, which records it in pyproject.toml and uv.lock. Using uv sync --inexact would also keep it, but the package would stay out of the project's declared dependencies.
Exercise
A CI job must fail if a developer changed pyproject.toml without updating uv.lock. Which flag should the job use with uv sync, and why not --frozen?
Show answer
Use uv sync --locked. It errors when the lockfile is out of date with pyproject.toml. --frozen skips that check and installs the old lock contents, so the mismatch would go unnoticed.