uv Projects: init, add & run
uv is a Python package and project manager from Astral, written in Rust. It started in 2024 as a fast pip replacement and grew into a single tool that covers pip, pip-tools, virtualenv, pipx, pyenv, twine and Poetry-style project management. It follows the standards ([project], [dependency-groups], PEP 517 builds, PEP 723 scripts), so a uv project is a plain Python project that other tools can still read.
Creating a project
uv init greet
cd greet
tree -a -I .git --noreport .
cat pyproject.toml
cat src/greet/__init__.pyInitialized project `greet` at `/home/learner/greet`
.
├── .gitignore
├── .python-version
├── README.md
├── pyproject.toml
└── src
└── greet
└── __init__.py
[project]
name = "greet"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
authors = [
{ name = "Ada Lovelace", email = "ada@example.com" }
]
requires-python = ">=3.13"
dependencies = []
[project.scripts]
greet = "greet:main"
[build-system]
requires = ["uv_build>=0.13.5,<0.14.0"]
build-backend = "uv_build"
def main() -> None:
print("Hello from greet!")
uv 0.13's default is a packaged application: the src layout, a console script greet pointing at greet:main, and the uv_build backend (pinned to a compatible range, since it's uv's own). It also ran git init, wrote a .gitignore and pinned the Python version in .python-version. Other shapes:
| Command | Creates |
|---|---|
uv init --no-package app |
an application that's never built: a main.py, no [build-system] |
uv init --lib greet |
a library: src/greet/__init__.py, py.typed, no console script |
uv init --bare |
only pyproject.toml |
uv init --script tool.py |
a single-file script with inline metadata (uv tool, uvx & scripts) |
uv run: lock, sync, run
uv run is the command you'll type most. Before running anything it makes sure the lockfile matches pyproject.toml and the .venv matches the lockfile, creating both if needed:
uv init -q greet && cd greet
uv run greet
echo "--- second run: nothing to do"
uv run greet
ls -AUsing CPython 3.13.17 interpreter at: /usr/local/bin/python3.13
Creating virtual environment at: .venv
Building greet @ file:///home/learner/greet
Built greet @ file:///home/learner/greet
Installed 1 package in 2ms
Hello from greet!
--- second run: nothing to do
Hello from greet!
.git
.gitignore
.python-version
.venv
README.md
pyproject.toml
src
uv.lock
The first run created .venv, resolved (one package: the project itself), wrote uv.lock, built and installed greet in editable mode, then ran its console script. The second run checked everything in milliseconds and went straight to running. There's no "activate" step and no way to forget to install something.
Adding dependencies
uv init -q greet && cd greet
uv add "requests>=2.31"
uv add --dev pytest 2>&1 | grep -E '^Resolved|pytest'
echo "--- pyproject.toml"
sed -n '/^dependencies/,/^]/p;/^\[dependency-groups\]/,$p' pyproject.toml
echo "--- the tree"
uv treeUsing CPython 3.13.17 interpreter at: /usr/local/bin/python3.13
Creating virtual environment at: .venv
Resolved 6 packages in 181ms
Building greet @ file:///home/learner/greet
Built greet @ file:///home/learner/greet
Prepared 6 packages in 17ms
Installed 6 packages in 14ms
+ certifi==2026.10.23
+ charset-normalizer==3.5.2
+ greet==0.1.0 (from file:///home/learner/greet)
+ idna==3.20
+ requests==2.34.2
+ urllib3==2.8.0
Resolved 12 packages in 64ms
+ pytest==9.1.1
--- pyproject.toml
dependencies = [
"requests>=2.31",
]
[dependency-groups]
dev = [
"pytest>=9.1.1",
]
--- the tree
Resolved 12 packages in 1ms
greet v0.1.0
├── requests v2.34.2
│ ├── certifi v2026.10.23
│ ├── charset-normalizer v3.5.2
│ ├── idna v3.20
│ └── urllib3 v2.8.0
└── pytest v9.1.1 (group: dev)
├── iniconfig v2.3.1
├── packaging v26.3
├── pluggy v1.7.0
└── pygments v2.21.0
uv addwrites the requirement you typed. With no version it writes a lower bound on the newest version ("pytest>=9.1.1"above), not Poetry's caret.--devputs the package into the standard[dependency-groups] devgroup;--group lintand--optional fancywork like Poetry's groups and extras.uv treeprints the resolved graph from the lockfile;(group: dev)marks dev-only branches.
Note: In this course's container,
uv addalso writes a[[tool.uv.index]]table with the local index URL intopyproject.toml(the sessions above hide it). uv does that because the container sets the default index throughUV_DEFAULT_INDEX; against plain PyPI nothing is added.
Running other things
uv run runs any command in the project environment, and --with layers extra packages on top for that one run without touching pyproject.toml:
uv init -q --no-package app && cd app
uv add -q requests==2.31.0
uv run python -c 'import requests; print("requests", requests.__version__)'
uv run main.py
uv run --with rich==13.9.4 python -c 'import importlib.metadata as m; print("rich", m.version("rich"), "just for this run")'
uv run python -c 'import rich' 2>&1 | tail -1
uv remove requests 2>&1 | grep -E '^ -'requests 2.31.0
Hello from app!
Downloading pygments (1.2MiB)
Downloaded pygments
Installed 4 packages in 69ms
rich 13.9.4 just for this run
ModuleNotFoundError: No module named 'rich'
- certifi==2026.10.23
- charset-normalizer==3.5.2
- idna==3.20
- requests==2.31.0
- urllib3==2.8.0
uv remove removed requests and its four dependencies, because the environment is synced to the lock after every change.
uv and Poetry, side by side
| Task | Poetry 2.5 | uv 0.13 |
|---|---|---|
| new project | poetry new greet |
uv init greet |
| add / remove | poetry add x, poetry remove x |
uv add x, uv remove x |
| dev dependency | poetry add --group dev x |
uv add --dev x |
| install from lock | poetry install / poetry sync |
uv sync |
| run in the env | poetry run cmd |
uv run cmd |
| update one package | poetry update x |
uv lock --upgrade-package x |
| build / publish | poetry build / poetry publish |
uv build / uv publish |
| default backend | poetry-core | uv_build |
| lockfile | poetry.lock |
uv.lock |
| manages Python itself | no | yes (uv python) |
Test yourself
What does uv run greet do in a fresh project before running the command?
- Nothing, it just runs
- Creates .venv if needed, locks if uv.lock is missing or stale, syncs the environment to the lock, then runs
- Asks you to activate the environment
- Publishes the project
What does uv run --with rich python script.py change in pyproject.toml?
- Adds rich to dependencies
- Adds rich to the dev group
- Nothing: rich is available only for that run
- It pins rich in uv.lock
Which uv command creates an application that is never built as a package (just a main.py)?
- uv init --lib
- uv init --no-package
- uv init --bare
- uv init --package