pypa/pip · error · CommandError
<dynamic: PEP723 exception message (exc.msg)>
Error message
<dynamic: PEP723 exception message (exc.msg)>
What it means
Raised by RequirementCommand.get_requirements() in req_command.py:406 when pep723_metadata() raises a PEP723Exception while parsing the inline metadata block from a script passed to --requirements-from-script. The PEP723Exception is caught and re-raised as a CommandError with exc.msg. Possible causes include: no PEP 723 block found, multiple blocks found, or invalid TOML inside the block.
Source
Thrown at src/pip/_internal/cli/req_command.py:406
isolated=options.isolated_mode,
user_supplied=True,
config_settings=(
parsed_req.options.get("config_settings")
if parsed_req.options
else None
),
)
requirements.append(req_to_add)
if options.requirements_from_scripts:
if len(options.requirements_from_scripts) > 1:
raise CommandError("--requirements-from-script can only be given once")
script = options.requirements_from_scripts[0]
try:
script_metadata = pep723_metadata(script)
except PEP723Exception as exc:
raise CommandError(exc.msg)
script_requires_python = script_metadata.get("requires-python", "")
if script_requires_python and not options.ignore_requires_python:
target_python = make_target_python(options)
if not check_requires_python(
requires_python=script_requires_python,
version_info=target_python.py_version_info,
):
raise UnsupportedPythonVersion(
f"Script {script!r} requires a different Python: "
f"{target_python.py_version} not in {script_requires_python!r}"
)
for req in script_metadata.get("dependencies", []):
req_to_add = install_req_from_req_string(
req,View on GitHub (pinned to d7d0d0a394)
Solutions
- Ensure the script contains exactly one `# /// script` block with valid TOML.
- Validate the TOML content separately: `python -c "import tomllib; tomllib.loads(open('block.txt').read())"`.
- Check for common TOML errors: unquoted strings, trailing commas, invalid key names.
- If the script has no dependencies, add an empty block: `# /// script\n# ///`.
Example fix
# before (missing or malformed PEP 723 block) # myscript.py has no metadata block pip install --requirements-from-script myscript.py # after # myscript.py contains: # /// script # dependencies = ["requests", "rich"] # /// pip install --requirements-from-script myscript.py
Defensive patterns
Strategy: validation
Validate before calling
import re
from pip._internal.utils.compat import tomllib
def validate_pep723_script(filepath):
"""Validate a script has exactly one parseable PEP 723 block."""
regex = r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$"
with open(filepath, encoding='utf8') as f:
script = f.read()
matches = [m for m in re.finditer(regex, script) if m.group('type') == 'script']
if len(matches) == 0:
return False, 'No PEP 723 script block found'
if len(matches) > 1:
return False, 'Multiple PEP 723 script blocks found'
content = ''.join(l[2:] if l.startswith('# ') else l[1:]
for l in matches[0].group('content').splitlines(keepends=True))
try:
tomllib.loads(content)
except Exception as e:
return False, f'Invalid TOML: {e}'
return True, None Try / catch
from pip._internal.exceptions import CommandError
try:
# pip install --requirements-from-script script.py
except CommandError as e:
# check if it's a PEP723 parse error
if 'metadata' in str(e).lower() or 'TOML' in str(e).lower():
# fix the script's PEP 723 block
Prevention
- Validate PEP 723 blocks with a TOML linter before using them with pip.
- Use the exact `# /// script` block format from PEP 723 spec.
- Test script metadata parsing independently before pip install.
When it happens
Trigger: Calling `pip install --requirements-from-script myscript.py` where myscript.py either: (a) has no `# /// script` PEP 723 metadata block, (b) has multiple such blocks, or (c) has a block with malformed TOML content (e.g., invalid syntax, unparseable keys).
Common situations: Forgetting to add the PEP 723 metadata block to a script. Malformed TOML in the dependencies list (e.g., missing quotes, invalid syntax). Copy-pasting a partial PEP 723 block. Using a script that was intended for a different inline metadata type.
Related errors
- --requirements-from-script can only be given once
- Script {script!r} requires a different Python: {target_pytho
- Multiple {name!r} blocks found in {scriptfile!r}
- Failed to parse TOML in {scriptfile!r}
- File does not contain {name!r} metadata: {scriptfile!r}
AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04).
Data as JSON: /data/errors/8de4c4c12c472696.json.
Report an issue: GitHub.