Sub topic 15
Single-file scripts with inline dependencies
A script often needs one or two third-party packages. Creating a project, a pyproject.toml, and a virtual environment for that is more setup than the script deserves. Inline script metadata lets the script declare its own Python version and dependencies in a comment block at the top of the file. uv reads that block, builds a temporary environment, and runs the script in it.
This chapter assumes you know how uv run works in a project, what requires-python means, and how dependency specifiers such as httpx>=0.27 are written.
The metadata format
Inline metadata is defined by PEP 723. It is a comment block that starts with # /// script and ends with # ///. Every line between the markers is a comment line whose content is TOML.
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "httpx>=0.27",
# "rich",
# ]
# ///
import httpx
from rich import print
response = httpx.get("https://example.com")
print(f"[green]{response.status_code}[/green] {len(response.text)} bytes")Run it with:
uv run script.pyThe keys match the [project] table of pyproject.toml, but only a few apply. You can set requires-python and dependencies. A script has no name, version, or build backend, because it is never built or installed. You can also add a [tool.uv] table for uv-specific settings.
The block follows these rules:
- The type must be
script. Other tools can define other block types. - Each line must start with
#. A space after the#is conventional. - A file may contain only one
scriptblock. - The block can appear anywhere in the file, but it conventionally goes at the top.
What uv does when you run the script
Four details of this flow cause most of the confusion.
- The environment is not
.venv. uv keeps script environments in its cache and reuses them when the metadata resolves to the same packages. The first run is slow and later runs start almost instantly. - A script with a metadata block ignores the surrounding project. If you run it from inside a project directory, uv does not install that project's dependencies. A script without a block runs in the project environment.
- uv picks an interpreter that satisfies
requires-python. If none is installed, uv can download one, as it does for projects. - The script gets no lockfile by default. Each resolution takes the newest versions that match your specifiers.
Creating and editing metadata with uv
You can edit the block by hand, but uv has commands for it:
uv init --script report.py --python 3.12
uv add --script report.py httpx rich
uv remove --script report.py richuv init --script creates the file with an empty metadata block. uv add --script writes dependencies into the block and creates one if the file has none. uv remove --script takes entries out. These commands don't install anything, because the environment is built when you run the script.
Ad-hoc dependencies without editing the file
Sometimes you want to try a package once. --with adds dependencies for a single run:
uv run --with rich script.py
uv run --with "httpx>=0.27" --python 3.13 script.py--with adds to whatever the metadata block declares. --python overrides the interpreter choice for that run.
If you run a script that has no metadata block and sits inside a project, add --no-project. uv then skips the project environment:
uv run --no-project --with rich script.pyMaking the script directly executable
A shebang line lets you run the file as a command on Linux and macOS:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = ["httpx"]
# ///
import httpx
print(httpx.get("https://example.com").status_code)chmod +x script.py
./script.pyThe -S flag tells env to split the rest of the line into separate arguments. Without it, most systems treat uv run --script as one program name and the shebang fails. The --script flag tells uv to treat the file as a script even if it has no .py extension.
Pinning for reproducibility
Because the script has no lockfile by default, the same file can behave differently next month when a dependency releases a new version. You have two ways to control this.
The first is a lockfile next to the script:
uv lock --script script.pyThis writes script.py.lock. Later uv run script.py calls use the locked versions. Commit the lockfile with the script.
The second is a date cutoff. The exclude-newer setting ignores any package version uploaded after the given timestamp:
# /// script
# requires-python = ">=3.12"
# dependencies = ["httpx"]
#
# [tool.uv]
# exclude-newer = "2026-01-01T00:00:00Z"
# ///
import httpx
print(httpx.__version__)This costs little and works well for scripts you share, since anyone who runs the file resolves the same set of versions.
The [tool.uv] table in a script can also declare [tool.uv.sources] and custom indexes, using the same syntax as in a project.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError` for a package you declared | The metadata block is malformed, so uv ignored it | Check that the markers are exactly `# /// script` and `# ///`, and that every line in between starts with `#` |
| Project dependencies are missing in the script | The script has a metadata block, so uv skips the project | Add the dependencies to the script block |
| Script behaves differently on another machine | Dependencies resolved to different versions | Use `uv lock --script` or `exclude-newer` |
| Shebang fails with "No such file or directory" | `-S` is missing from the `env` line | Use `#!/usr/bin/env -S uv run --script` |
| TOML parse error | A comma or quote is missing inside the block | Remember that the content must be valid TOML once the `# ` prefix is removed |
When to use a script and when to use a project
Choose a script when the code fits in one file, you want to share it as a single file, and it needs no build or tests. Move to a project when the code grows into several modules, needs a command-line entry point, or needs development dependency groups. A project also gives you a shared lockfile for the whole codebase. Moving is a small step: copy the script's dependencies and requires-python into the [project] table.
Exercise
A script has a valid inline metadata block that lists httpx. You run uv run script.py from inside a project whose pyproject.toml lists typer, and the script runs import typer. Does the import succeed? How do you fix it if it does not?
Show answer
The import fails. A script with a metadata block runs in its own cached environment, and uv ignores the project's dependencies. Fix it by running uv add --script script.py typer so the block lists typer. For a single run you could use uv run --with typer script.py.
Exercise
You share a script with a colleague, and two months later it fails because a dependency released a breaking version. Name two ways to prevent this.
Show answer
Run uv lock --script script.py and commit script.py.lock next to the script. Alternatively, set exclude-newer in a [tool.uv] table inside the metadata block so uv ignores versions uploaded after that date. You could also pin exact versions in dependencies, but that does not pin transitive dependencies.