Sub topic 15
Single-file scripts with inline dependencies
This chapter assumes you know basic Python, can use a terminal, and understand what a virtual environment and a dependency are. You do not need a full project to use a third-party package. uv can read dependency information from a comment block at the top of a script and run the script in a temporary environment.
The inline metadata block
The block follows a standard format (PEP 723). It starts with # /// script, ends with # ///, and holds TOML in between. Every line inside the block begins with # .
# /// script
# requires-python = ">=3.12"
# dependencies = ["httpx"]
# ///
import httpx
print(httpx.get("https://example.com").status_code)The two keys match the ones in the [project] table of pyproject.toml:
requires-pythonsets the Python versions the script supports.dependencieslists the packages the script imports.
Save the file as script.py and run it:
uv run script.pyuv reads the block, resolves httpx, and builds a cached environment. It then runs the script in that environment. Your system Python and any project environment stay unchanged.
How it works
Because the environment is cached, a second run starts quickly. If you edit dependencies, uv builds a new environment on the next run.
Managing the block with uv
You can edit the block by hand, or let uv do it:
uv init --script script.py --python 3.12
uv add --script script.py httpx
uv remove --script script.py httpxuv init --script creates a file with a metadata block. uv add --script and uv remove --script change the dependencies list in place. This is the script version of uv add and uv remove for projects.
Scripts and projects
If you run uv run script.py inside a project directory, uv uses the script's metadata and ignores the project's dependencies. Packages that only the project declares are not available to the script. This surprises people who expect the project environment to apply.
You can add a [tool.uv] table to the block for settings such as exclude-newer. That setting limits resolution to packages published before a given date, which helps a script stay reproducible over time. Scripts do not get a uv.lock file by default. Use uv lock --script script.py if you want one.
Common errors
| Symptom | Likely cause |
|---|---|
| `ModuleNotFoundError` for a package you listed | The block is malformed, for example a line without the leading `# `, so uv ignored it |
| The wrong Python version is used | `requires-python` is missing or too loose |
| A project dependency is missing in the script | The script runs in its own environment, so list it in the block |
Exercise
You run uv run report.py inside a project that lists pandas in its dependencies. The report.py metadata block lists only httpx, and the script imports pandas. What happens, and how do you fix it?
Show answer
The script fails with ModuleNotFoundError for pandas. uv uses the inline metadata and ignores the project's dependencies. Fix it by running uv add --script report.py pandas, which adds pandas to the block.