astral-sh/uv · error · anyhow::Error

`{}` does not contain inline script metadata

Error message

`{}` does not contain inline script metadata

What it means

Thrown in from_source_with_cache (crates/uv-requirements/src/specification.rs:325) when a RequirementsSource::Pep723Script's content parses without a PEP 723 metadata block — Pep723Metadata::parse returns Ok(None). The source was explicitly declared to be an inline-metadata script (e.g. `uv run script.py` resolving it as such), so uv errors rather than silently treating it as dependency-free.

Source

Thrown at crates/uv-requirements/src/specification.rs:325

                Self {
                    source_trees: vec![SourceTree::PyProjectToml(path.clone(), pyproject_toml)],
                    ..Self::default()
                }
            }
            RequirementsSource::Pep723Script(path) => {
                let content = if let Some(content) = cache.get(path.as_path()) {
                    content.clone()
                } else {
                    let content = read_file(path, client_builder).await?;
                    cache.insert(path.clone(), content.clone());
                    content
                };

                let metadata = match Pep723Metadata::parse(content.as_bytes()) {
                    Ok(Some(script)) => script,
                    Ok(None) => {
                        return Err(anyhow::anyhow!(
                            "`{}` does not contain inline script metadata",
                            path.user_display(),
                        ));
                    }
                    Err(err) => return Err(err.into()),
                };

                Self::from_pep723_metadata(&metadata)
            }
            RequirementsSource::SetupPy(path) => {
                if !path.is_file() {
                    return Err(anyhow::anyhow!("File not found: `{}`", path.user_display()));
                }

                Self {
                    source_trees: vec![SourceTree::SetupPy(path.clone())],
                    ..Self::default()
                }

View on GitHub (pinned to f1a42680ff)

Solutions

  1. Add a PEP 723 block to the script: `uv init --script script.py` scaffolds it
  2. Manually insert the block starting with `# /// script` and listing `dependencies = ["flask"]` inside as TOML comments
  3. If the script needs no metadata, pass its dependencies separately (e.g. `uv run --with flask script.py`)

Example fix

# before: script.py has no metadata block
# after: script.py
# /// script
# requires-python = ">=3.12"
# dependencies = ["flask"]
# ///
import flask
Defensive patterns

Strategy: validation

Validate before calling

fn has_pep723_block(content: &str) -> bool {
    // PEP 723 block must appear before the first statement; simple pre-check:
    content.lines().take_while(|l| l.starts_with("#!") || l.trim().is_empty())
        .count();
    content.contains("# /// script")
}

let content = std::fs::read_to_string(&path)?;
if !has_pep723_block(&content) {
    // treat as a plain script or require metadata before creating a Pep723Script source
}

Try / catch

match RequirementsSpecification::from_source(src, &client_builder).await {
    Err(err) if err.to_string().contains("does not contain inline script metadata") => {
        // fall back: run script with explicit --with flags instead of metadata
    }
    spec => spec?,
}

Prevention

When it happens

Trigger: `uv run --with-requirements` style flows or programmatic RequirementsSource::Pep723Script(path) where the file lacks a `# /// script` TOML block; PEP 723 requires the metadata comment block (`# /// script\n# dependencies = [...]`) and parse returns Ok(None) when absent.

Common situations: A script that once had inline metadata was stripped (formatter, copy-paste, minifier); the user renamed a requirements.txt to .py expecting uv to read it; the `# /// script` fence was mangled (e.g. wrong number of slashes or block key).

Related errors


AI-assisted analysis of astral-sh/uv@f1a42680ff (2026-08-16). Data as JSON: /api/errors/8fe8934adb2e4b90. Report an issue: GitHub.