{"record":{"id":"7ce0055616222349","repo":"pypa/pip","slug":"file-does-not-contain-name-r-metadata-scriptfi","errorCode":null,"errorMessage":"File does not contain {name!r} metadata: {scriptfile!r}","messagePattern":"File does not contain (.+?) metadata: (.+?)","errorType":"validation","errorClass":"PEP723Exception","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/req/pep723.py","lineNumber":37,"sourceCode":"\n    name = \"script\"\n    matches = list(\n        filter(lambda m: m.group(\"type\") == name, re.finditer(REGEX, script))\n    )\n\n    if len(matches) > 1:\n        raise PEP723Exception(f\"Multiple {name!r} blocks found in {scriptfile!r}\")\n    elif len(matches) == 1:\n        content = \"\".join(\n            line[2:] if line.startswith(\"# \") else line[1:]\n            for line in matches[0].group(\"content\").splitlines(keepends=True)\n        )\n        try:\n            metadata = tomllib.loads(content)\n        except Exception as exc:\n            raise PEP723Exception(f\"Failed to parse TOML in {scriptfile!r}\") from exc\n    else:\n        raise PEP723Exception(\n            f\"File does not contain {name!r} metadata: {scriptfile!r}\"\n        )\n\n    return metadata\n","sourceCodeStart":19,"sourceCodeEnd":42,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/req/pep723.py#L19-L42","documentation":"pip's PEP 723 support was invoked on a script file, but the file contains no '# /// script' inline metadata block at all. Without this block, pip cannot determine which dependencies to install for the script.","triggerScenarios":"Running a plain Python script through pip's inline-script execution (pip run) where the script has no '# /// script' block. The regex REGEX in pep723.py found zero matches with type 'script'.","commonSituations":"Forgetting to add inline metadata to a script before running it with pip. Running a script that was meant to use a virtual environment's pre-installed packages. Script that predates PEP 723 adoption.","solutions":["Add a PEP 723 metadata block to the script with the required dependencies","Alternatively, pre-install the dependencies in your environment and run the script directly with 'python script.py' instead of pip"],"exampleFix":"# before — script.py has no metadata block\nimport requests\nprint(requests.__version__)\n\n# after\n# /// script\n# dependencies = [\"requests\"]\n# ///\nimport requests\nprint(requests.__version__)","handlingStrategy":"validation","validationCode":"import re\n\nREGEX = r\"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\\s(?P<content>(^#(| .*)$\\s)+)^# ///$\"\n\ndef has_pep723_metadata(filepath: str) -> bool:\n    with open(filepath) as f:\n        script = f.read()\n    return any(m.group(\"type\") == \"script\" for m in re.finditer(REGEX, script))\n\nif not has_pep723_metadata(\"script.py\"):\n    print(\"No PEP 723 metadata block found — add one or install deps manually\")","typeGuard":"import re\n\ndef contains_pep723_block(filepath: str) -> bool:\n    \"\"\"Type guard: True if the script has a '# /// script' metadata block.\"\"\"\n    regex = r\"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\\s(?P<content>(^#(| .*)$\\s)+)^# ///$\"\n    with open(filepath) as f:\n        return any(m.group(\"type\") == \"script\" for m in re.finditer(regex, f.read()))","tryCatchPattern":"from pip._internal.req.pep723 import pep723_metadata, PEP723Exception\n\ntry:\n    meta = pep723_metadata(\"script.py\")\nexcept PEP723Exception as e:\n    if \"does not contain\" in e.msg:\n        print(\"Add a '# /// script' block with dependencies\")\n    else:\n        raise","preventionTips":["Add a '# /// script' block before running scripts through pip's PEP 723 support","If the script has no external deps, run it directly with 'python script.py'","Use a template/snippet for the PEP 723 block to avoid forgetting it"],"tags":["pip","pep723","inline-script","metadata"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}