{"id":"8e26bfb40687c397","repo":"pypa/pip","slug":"script-script-r-requires-a-different-python-ta","errorCode":null,"errorMessage":"Script {script!r} requires a different Python: {target_python.py_version} not in {script_requires_python!r}","messagePattern":"Script (.+?) requires a different Python: (.+?) not in (.+?)","errorType":"exception","errorClass":"UnsupportedPythonVersion","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/cli/req_command.py","lineNumber":417,"sourceCode":"            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,\n                    isolated=options.isolated_mode,\n                    user_supplied=True,\n                )\n                requirements.append(req_to_add)\n\n        if options.require_hashes and options.no_require_hashes:\n            raise CommandError(\n                \"--require-hashes and --no-require-hashes are mutually exclusive\"\n            )\n\n        # If any requirement has hash options, enable hash checking for all","sourceCodeStart":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/cli/req_command.py#L399-L435","documentation":"Raised by RequirementCommand.get_requirements() in req_command.py:417 when a PEP 723 script's requires-python field is incompatible with the target Python version (derived from --python-version or the running interpreter). The check uses check_requires_python() against the script's declared Requires-Python specifier. This prevents installing dependencies that won't work with the interpreter being used.","triggerScenarios":"Calling `pip install --requirements-from-script myscript.py` where myscript.py has `requires-python = \">=3.11\"` in its PEP 723 block, but the running Python (or --python-version) is 3.10 or earlier. The check at req_command.py:410-416 is skipped if --ignore-requires-python is set.","commonSituations":"Running a script designed for a newer Python on an older interpreter. CI images with mismatched Python versions. Development environment Python version doesn't match the script's requirements. Script was written for a different Python than the developer's local setup.","solutions":["Upgrade to a Python version that satisfies the script's requires-python constraint.","Use `--python-version` to target the correct version if cross-installing.","Add `--ignore-requires-python` to bypass the check (at your own risk).","Update the script's requires-python field if the constraint is too strict."],"exampleFix":"# before\n# myscript.py has: requires-python = \">=3.12\"\n# running Python 3.10\npip install --requirements-from-script myscript.py\n\n# after\n# Option A: use the right Python\npython3.12 -m pip install --requirements-from-script myscript.py\n# Option B: bypass the check\npip install --ignore-requires-python --requirements-from-script myscript.py","handlingStrategy":"validation","validationCode":"import sys, re\nfrom pip._internal.utils.compat import tomllib\nfrom pip._internal.utils.packaging import check_requires_python\n\ndef validate_script_python_compat(filepath, python_version=None):\n    \"\"\"Check if the running Python satisfies the script's requires-python.\"\"\"\n    from pip._internal.req.pep723 import pep723_metadata\n    metadata = pep723_metadata(filepath)\n    requires_python = metadata.get('requires-python', '')\n    if not requires_python:\n        return True\n    version = python_version or sys.version_info[:2]\n    return check_requires_python(requires_python=requires_python, version_info=version)","typeGuard":null,"tryCatchPattern":"from pip._internal.exceptions import UnsupportedPythonVersion\ntry:\n    # pip install --requirements-from-script script.py\nexcept UnsupportedPythonVersion as e:\n    # script requires newer/older Python; upgrade interpreter or add --ignore-requires-python\n","preventionTips":["Match your Python version to the script's requires-python before installing.","Use pyenv to manage multiple Python versions.","Keep requires-python constraints accurate in PEP 723 scripts.","Use --ignore-requires-python only when you understand the risks."],"tags":["cli","pep723","python-version","requires-python","compatibility","requirements-from-script"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}