Python uv Common Patterns: Dependencies, uvx & Script Execution

2 min read

This article focuses on the five most common daily development tasks, with practical templates you can use right away. Master these patterns and you can handle 80% of scenarios — the remaining 20% is for the advanced section.

Pattern 1: Adding and Removing Dependencies

# Add a dependency
uv add requests
uv add "requests>=2.31,<3"   # with version constraints
 
# Dev dependencies (tests, lint, type checking)
uv add --dev pytest ruff mypy
 
# Remove
uv remove requests

uv add simultaneously updates pyproject.toml, uv.lock, and .venv — one command does it all.

Pattern 2: uv run to Execute Project Commands

For scripts, CLIs, and tests inside the project, always use uv run:

uv run python main.py
uv run pytest
uv run ruff check .
uv run mypy src/

No activation needed, no worrying about the wrong Python.

Pattern 3: uvx to Run Standalone CLI Tools

Want to run a tool temporarily without adding it to the project? Use uvx (equivalent to uv tool run):

# On-demand, disposable
uvx ruff check .
uvx pycowsay "hello uv"
 
# Pin a version
uvx ruff@0.5.0 check
 
# When the package name and command differ
uvx --from httpie http https://httpbin.org/get

uvx creates a temporary environment to run the tool without polluting your project — like sampling food without buying the whole package. Perfect for "I just want to try this" moments.

Pattern 4: Inline Script Dependencies (PEP 723)

Single script, don't want to create a project? Use inline script metadata:

# /// script
# dependencies = ["requests", "rich"]
# ///
 
import requests
from rich.pretty import pprint
 
r = requests.get("https://api.github.com/zen")
pprint(r.text)
uv add --script fetch.py requests rich
uv run fetch.py

Or in one line:

uv run --with requests --with rich fetch.py

Pattern 5: Migrating from requirements.txt

Existing project with a requirements.txt? Take it in directly:

uv add -r requirements.txt

Or compile to a lockfile then sync:

uv pip compile requirements.in -o requirements.txt
uv pip sync requirements.txt

After migrating, treat pyproject.toml as the primary source — requirements.txt can be used as an export format.

Next Steps

Once you've practiced the daily patterns, if you want to try Python version switching, the pip interface, or a monorepo setup: 👉 Advanced