brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: uv add --script writes python dependencies into the script itself

Python
~/tips/tip-python-script-metadata-uv-add

uv add --script fetch.py requests

Stores a script's dependencies in a comment block so one file runs anywhere with uv.

A single-file script should carry its own requirements, so I let uv write them into the file header.

uv init --script fetch.py
uv add --script fetch.py requests
uv run fetch.py

uv add --script inserts an inline metadata block (PEP 723, a comment starting with # /// script) listing the dependencies and the required python version. uv run fetch.py reads that block, builds a cached environment and runs the script. The format is a standard, so other tools such as pipx can read it too. Plain python3 fetch.py ignores the block and uses whatever is installed, which can fail if the package is missing.

Further reading: uv documentation: running scripts

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link