pypa/pip · error · PEP723Exception
File does not contain
Error message
File does not contain {name!r} metadata: {scriptfile!r} What it means
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.
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
Example fix
# before — script.py has no metadata block import requests print(requests.__version__) # after # /// script # dependencies = ["requests"] # /// import requests print(requests.__version__)
Defensive patterns
Strategy: validation
Validate before calling
import re
REGEX = r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$"
def has_pep723_metadata(filepath: str) -> bool:
with open(filepath) as f:
script = f.read()
return any(m.group("type") == "script" for m in re.finditer(REGEX, script))
if not has_pep723_metadata("script.py"):
print("No PEP 723 metadata block found — add one or install deps manually") Type guard
import re
def contains_pep723_block(filepath: str) -> bool:
"""Type guard: True if the script has a '# /// script' metadata block."""
regex = r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$"
with open(filepath) as f:
return any(m.group("type") == "script" for m in re.finditer(regex, f.read())) Try / catch
from pip._internal.req.pep723 import pep723_metadata, PEP723Exception
try:
meta = pep723_metadata("script.py")
except PEP723Exception as e:
if "does not contain" in e.msg:
print("Add a '# /// script' block with dependencies")
else:
raise Prevention
- 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
When it happens
Trigger: 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'.
Common situations: 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.
Related errors
- Multiple blocks found in
- Failed to parse TOML in
- Invalid requirement
- Requested has inconsistent Name between its PEP 658…
- Requested has inconsistent Name: expected , but metadata has
AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08).
Data as JSON: /api/errors/7ce0055616222349.
Report an issue: GitHub.
Appendix: source
Thrown at src/pip/_internal/req/pep723.py:37
name = "script"
matches = list(
filter(lambda m: m.group("type") == name, re.finditer(REGEX, script))
)
if len(matches) > 1:
raise PEP723Exception(f"Multiple {name!r} blocks found in {scriptfile!r}")
elif len(matches) == 1:
content = "".join(
line[2:] if line.startswith("# ") else line[1:]
for line in matches[0].group("content").splitlines(keepends=True)
)
try:
metadata = tomllib.loads(content)
except Exception as exc:
raise PEP723Exception(f"Failed to parse TOML in {scriptfile!r}") from exc
else:
raise PEP723Exception(
f"File does not contain {name!r} metadata: {scriptfile!r}"
)
return metadata
View on GitHub (pinned to f399c37189)