19 uv: uv run and activating an environment
Learning Objectives
- Activate and deactivate a project’s virtual environment, and explain what activating changes
- Explain why
uv runis a shortcut for activating, running one command, and deactivating - Explain what
uv rundoes that activating does not, and choose between them
19.1 Two ways to use an environment
The uv chapters run everything through uv run:
- scripts,
uv run penguins.py - JupyterLab,
uv run jupyter lab - Quarto,
uv run quarto render - tests,
uv run pytest
Many tutorials, and some tools, activate the environment instead.
This chapter shows what each one does, using the penguin_analysis project from uv: projects, and gives you a good way to think about uv run.
19.2 Activating, running, deactivating
In a new terminal, python3 is whatever Python your computer finds first. On this Mac that is Homebrew’s:
Terminal
which python3Output
/opt/homebrew/bin/python3
Activating the project’s environment changes that:
Terminal
source .venv/bin/activateTerminal
source .venv/Scripts/activate.venv\Scripts\Activate.ps1Your prompt now starts with (penguin-analysis), and every command you type finds the environment first:
Terminal
which python3
echo $VIRTUAL_ENVOutput
/Users/dan/penguin_analysis/.venv/bin/python3
/Users/dan/penguin_analysis/.venv
Activating did two things. It put .venv/bin first on your PATH (.venv\Scripts on Windows), and it set the VIRTUAL_ENV environment variable to the environment’s folder. Python’s own documentation describes these two changes in How venvs work. Now python penguins.py, jupyter lab, and quarto render all use the project’s packages:
Terminal
python penguins.pyOutput
species island bill_length_mm ... body_mass_g sex year
0 Adelie Torgersen 39.1 ... 3750.0 male 2007
1 Adelie Torgersen 39.5 ... 3800.0 female 2007
...
The environment stays active until you deactivate it or close the terminal:
Terminal
deactivate
which python3Output
/opt/homebrew/bin/python3
19.3 A good way to think about uv run
The uv documentation says that uv run runs a command in the project environment, and that before every uv run, uv checks that uv.lock matches pyproject.toml and that .venv matches uv.lock (Running commands). The same guide gives the alternative to uv run. You run uv sync, then activate the environment, then run your command.
So a good way to think about uv run <command> is as a shortcut for four steps:
uv sync, so.venvmatchesuv.lock- activate the environment
- run the command
- deactivate the environment
The uv documentation does not describe how uv run sets up the environment internally. You can see, though, that for the command it runs, it changes the same two things activating does:
Terminal
uv run python -c "import os, sys; print(sys.executable); print(os.environ['VIRTUAL_ENV'])"
which python3Output
/Users/dan/penguin_analysis/.venv/bin/python
/Users/dan/penguin_analysis/.venv
/opt/homebrew/bin/python3
Inside uv run, Python is the project’s, and VIRTUAL_ENV names the project’s .venv. Straight afterwards, python3 is Homebrew’s again. There is nothing to deactivate, because nothing stayed active.
So these do the same work:
Terminal
uv sync
source .venv/bin/activate
python penguins.py
deactivateTerminal
uv sync
source .venv/Scripts/activate
python penguins.py
deactivateuv sync
.venv\Scripts\Activate.ps1
python penguins.py
deactivateTerminal
uv run penguins.py19.4 What uv run does that activating does not
Before it runs your command, uv run makes sure .venv matches uv.lock, and creates .venv if it does not exist yet (uv’s uv run reference says “the project environment will be created and updated before invoking the command”). Activating only switches to whatever .venv already holds.
The difference shows in a fresh clone of the project, which has pyproject.toml, uv.lock, and the code, but no .venv:
Terminal
source .venv/bin/activateOutput
bash: .venv/bin/activate: No such file or directory
Terminal
uv run penguins.pyOutput
Using CPython 3.14.3
Creating virtual environment at: .venv
Installed 5 packages in 72ms
species island bill_length_mm ... body_mass_g sex year
0 Adelie Torgersen 39.1 ... 3750.0 male 2007
1 Adelie Torgersen 39.5 ... 3800.0 female 2007
2 Adelie Torgersen 40.3 ... 3250.0 female 2007
3 Adelie Torgersen NaN ... NaN NaN 2007
4 Adelie Torgersen 36.7 ... 3450.0 female 2007
[5 rows x 8 columns]
| Activate, run, deactivate | uv run |
|
|---|---|---|
| Finds the environment’s Python and programs | yes | yes |
| How long it lasts | until deactivate or the terminal closes |
one command |
Checks .venv against uv.lock first |
no | yes |
Creates .venv if it is missing |
no | yes |
19.5 Which to use
Use uv run for scripts, JupyterLab, Quarto, and tests, and put those commands in your Makefile targets. You never have to remember which environment is active, and the environment always matches the lockfile.
Activating is fine when a tool expects an active environment, or when you want to type many commands in a row. If you do activate:
- run
uv syncfirst, so.venvmatchesuv.lock deactivatewhen you are done
A forgotten deactivate leaves the environment active for whatever you run next in that terminal, even in another project. uv: rendering Quarto documents shows that happening with a Quarto report.