{"id":"8de4c4c12c472696","repo":"pypa/pip","slug":"dynamic-pep723-exception-message-exc-msg","errorCode":null,"errorMessage":"<dynamic: PEP723 exception message (exc.msg)>","messagePattern":"<dynamic: PEP723 exception message \\(exc\\.msg\\)>","errorType":"exception","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/cli/req_command.py","lineNumber":406,"sourceCode":"                    isolated=options.isolated_mode,\n                    user_supplied=True,\n                    config_settings=(\n                        parsed_req.options.get(\"config_settings\")\n                        if parsed_req.options\n                        else None\n                    ),\n                )\n                requirements.append(req_to_add)\n\n        if options.requirements_from_scripts:\n            if len(options.requirements_from_scripts) > 1:\n                raise CommandError(\"--requirements-from-script can only be given once\")\n\n            script = options.requirements_from_scripts[0]\n            try:\n                script_metadata = pep723_metadata(script)\n            except PEP723Exception as exc:\n                raise CommandError(exc.msg)\n\n            script_requires_python = script_metadata.get(\"requires-python\", \"\")\n\n            if script_requires_python and not options.ignore_requires_python:\n                target_python = make_target_python(options)\n\n                if not check_requires_python(\n                    requires_python=script_requires_python,\n                    version_info=target_python.py_version_info,\n                ):\n                    raise UnsupportedPythonVersion(\n                        f\"Script {script!r} requires a different Python: \"\n                        f\"{target_python.py_version} not in {script_requires_python!r}\"\n                    )\n\n            for req in script_metadata.get(\"dependencies\", []):\n                req_to_add = install_req_from_req_string(\n                    req,","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/cli/req_command.py#L388-L424","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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# ///`."],"exampleFix":"# before (missing or malformed PEP 723 block)\n# myscript.py has no metadata block\npip install --requirements-from-script myscript.py\n\n# after\n# myscript.py contains:\n# /// script\n# dependencies = [\"requests\", \"rich\"]\n# ///\npip install --requirements-from-script myscript.py","handlingStrategy":"validation","validationCode":"import re\nfrom pip._internal.utils.compat import tomllib\n\ndef validate_pep723_script(filepath):\n    \"\"\"Validate a script has exactly one parseable PEP 723 block.\"\"\"\n    regex = r\"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\\s(?P<content>(^#(| .*)$\\s)+)^# ///$\"\n    with open(filepath, encoding='utf8') as f:\n        script = f.read()\n    matches = [m for m in re.finditer(regex, script) if m.group('type') == 'script']\n    if len(matches) == 0:\n        return False, 'No PEP 723 script block found'\n    if len(matches) > 1:\n        return False, 'Multiple PEP 723 script blocks found'\n    content = ''.join(l[2:] if l.startswith('# ') else l[1:]\n                      for l in matches[0].group('content').splitlines(keepends=True))\n    try:\n        tomllib.loads(content)\n    except Exception as e:\n        return False, f'Invalid TOML: {e}'\n    return True, None","typeGuard":null,"tryCatchPattern":"from pip._internal.exceptions import CommandError\ntry:\n    # pip install --requirements-from-script script.py\nexcept CommandError as e:\n    # check if it's a PEP723 parse error\n    if 'metadata' in str(e).lower() or 'TOML' in str(e).lower():\n        # fix the script's PEP 723 block\n","preventionTips":["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."],"tags":["cli","pep723","toml","parsing","requirements-from-script"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}