Sub topic 8
Environments, uv sync, and uv run
This chapter assumes you know what a virtual environment is, have a project created with uv init, and have dependencies added with uv add. It covers where uv puts the project environment and how uv sync and uv run keep it in line with your files.
The project environment
uv keeps one virtual environment per project, in a .venv directory next to pyproject.toml. You don't need to create or activate it. uv creates it the first time a command needs it, using the Python version allowed by requires-python and any .python-version pin.
The environment is a cache of your declared state. The sources of truth are pyproject.toml and uv.lock. If .venv is deleted or damaged, uv rebuilds it.
Syncing with uv sync
uv sync makes .venv match the lockfile. It installs missing packages, upgrades or downgrades changed ones, and removes packages that are no longer declared. The project itself is installed in editable mode, so changes in src/ take effect immediately.
uv sync
uv sync --no-dev
uv sync --group docs
uv sync --lockeduv syncincludes thedevgroup by default.--no-devleaves out thedevgroup, which suits production images.--group docsadds a named dependency group.--lockedfails ifuv.lockdoes not matchpyproject.toml, instead of updating it.
Running code with uv run
uv run executes a command inside the project environment. Before it starts the command, it checks that the lockfile is current and that the environment matches it. If either is stale, uv fixes it first.
uv run python -m weather_cli
uv run pytest -q
uv run weatherThe last line runs the console script defined under [project.scripts]. Because uv run syncs first, you can edit pyproject.toml, run a command, and get the new state without a separate install step.
Common errors
- Running
pythonorpytestdirectly. If.venvis not activated, you get the system interpreter andModuleNotFoundError. Useuv run pythonor activate.venvyourself. - A stale
.venvafter switching Python versions. If you change the pin, runuv sync. uv recreates the environment when the interpreter no longer fits. - Installing with
pip installinside.venv. The package is not recorded inpyproject.tomloruv.lock, so the nextuv syncremoves it. Useuv add. - Committing
.venv. Add it to.gitignore. Commituv.lockinstead.
Exercise
You run uv run pytest after adding httpx>=0.27 to dependencies by editing pyproject.toml directly. What does uv do before it runs the tests, and which flag would make the command fail instead of updating the lockfile?
Show answer
uv notices that uv.lock no longer matches pyproject.toml, re-resolves and rewrites the lockfile, installs httpx into .venv, and then runs pytest. Passing --locked (for example uv run --locked pytest) makes it fail instead of updating the lockfile.
Exercise
A teammate ran pip install requests inside .venv, and requests is not in pyproject.toml. What happens to it on the next uv sync, and how do you keep it properly?
Show answer
uv sync removes it, because the sync is exact and requests is not in the lockfile. To keep it, run uv add requests so it is recorded in pyproject.toml and uv.lock.