`uv run` makes it easy to manage adhoc scripts
When running python scripts with external dependency we usually have a requirements.txt or pyproject.toml. Based on this dependency list we setup our virtual environment and then we run script.
uv has a feature that we can use to install dependency at run time.
We can specify depes in the script itself
# /// script
# dependencies = [
# "httpx",
# ]
# ///
import httpx
resp = httpx.get("https://peps.python.org/api/peps.json")
data = resp.json()
print([(k, v["title"]) for k, v in data.items()][:10])
Or with --with flag
uv run --with httpx==0.26.0 python -c "import httpx; print(httpx.__version__)"