TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › uv Projects: init, add & run

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

bash · try it yourself
uv init greet
cd greet
tree -a -I .git --noreport .
cat pyproject.toml
cat src/greet/__init__.py
Initialized 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:

bash · try it yourself
uv init -q greet && cd greet
uv run greet
echo "--- second run: nothing to do"
uv run greet
ls -A
Using 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

bash · try it yourself
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 tree
Using 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 add writes 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.
  • --dev puts the package into the standard [dependency-groups] dev group; --group lint and --optional fancy work like Poetry's groups and extras.
  • uv tree prints the resolved graph from the lockfile; (group: dev) marks dev-only branches.

Note: In this course's container, uv add also writes a [[tool.uv.index]] table with the local index URL into pyproject.toml (the sessions above hide it). uv does that because the container sets the default index through UV_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:

bash · try it yourself
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?

  1. Nothing, it just runs
  2. Creates .venv if needed, locks if uv.lock is missing or stale, syncs the environment to the lock, then runs
  3. Asks you to activate the environment
  4. Publishes the project

What does uv run --with rich python script.py change in pyproject.toml?

  1. Adds rich to dependencies
  2. Adds rich to the dev group
  3. Nothing: rich is available only for that run
  4. It pins rich in uv.lock

Which uv command creates an application that is never built as a package (just a main.py)?

  1. uv init --lib
  2. uv init --no-package
  3. uv init --bare
  4. uv init --package