pypa/pip · error · UnsupportedPythonVersion

Script {script!r} requires a different Python: {py_version}

Error message

Script {script!r} requires a different Python: {py_version} not in {script_requires_python!r}

What it means

Raised as UnsupportedPythonVersion from RequirementCommand.get_requirements (req_command.py:417) when a PEP 723 script's requires-python metadata does not permit the target Python version. Pip parses the script's inline metadata via pep723_metadata (line 404), reads requires-python (line 408), and unless --ignore-requires-python is set, calls check_requires_python (line 413) against the target python (derived from options via make_target_python). A mismatch aborts the install with this message naming the script, the active version, and the required specifier.

Source

Thrown at src/pip/_internal/cli/req_command.py:417

            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,
                    isolated=options.isolated_mode,
                    user_supplied=True,
                )
                requirements.append(req_to_add)

        if options.require_hashes and options.no_require_hashes:
            raise CommandError(
                "--require-hashes and --no-require-hashes are mutually exclusive"
            )

        # If any requirement has hash options, enable hash checking for all

View on GitHub (pinned to f399c37189)

Solutions

  1. Run pip under an interpreter that satisfies the script's requires-python (use `pip --python <newer-interpreter> install --requirements-from-script tool.py`).
  2. If the constraint is wrong, edit the script's PEP 723 requires-python field to match the actual requirement.
  3. Bypass the check with `--ignore-requires-python` only if you understand the script may break on this Python.

Example fix

# before (active interpreter is 3.10)
pip install --requirements-from-script tool.py
# after
pip --python /usr/bin/python3.12 install --requirements-from-script tool.py
Defensive patterns

Strategy: validation

Validate before calling

# Check the script's requires-python against the target interpreter before installing.
from pip._internal.utils.compatibility_tags import check_requires_python
from pip._internal.req.req_file import pep723_metadata  # or equivalent parser
import sys
def script_python_ok(script):
    meta = pep723_metadata(script)
    rp = meta.get("requires-python", "")
    if rp and not check_requires_python(rp, sys.version_info):
        raise SystemExit(f"{script} requires {rp}; active is {sys.version_info[:2]}")
    return True

Prevention

When it happens

Trigger: `pip install --requirements-from-script tool.py` where tool.py's PEP 723 block says `# /// requires-python = >=3.11` but the active interpreter is 3.10.

Common situations: Running a tool script under an older interpreter than the script author intended; CI matrix mismatch between the runner's Python and the script's requires-python; inheriting a script that pins a newer Python.

Related errors


AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08). Data as JSON: /api/errors/cde3959d43ce8c2d. Report an issue: GitHub.