Skip to content

Commit

Permalink
Check python version in __pip-runner__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pfmoore committed Jul 31, 2022
1 parent ddfa05c commit 3333891
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ def get_version(rel_path: str) -> str:
],
},
zip_safe=False,
# NOTE: python_requires is duplicated in __pip-runner__.py.
# When changing this value, please change the other copy as well.
python_requires=">=3.7",
)
16 changes: 16 additions & 0 deletions src/pip/__pip-runner__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from typing import Optional, Sequence, Union

PIP_SOURCES_ROOT = dirname(dirname(__file__))
# Copied from setup.py
PYTHON_REQUIRES = ">=3.7"


class PipImportRedirectingFinder:
Expand All @@ -30,8 +32,22 @@ def find_spec(
return spec


def check_python_version() -> None:
# Import here to ensure the imports happen after the sys.meta_path change.
from pip._vendor.packaging.specifiers import SpecifierSet
from pip._vendor.packaging.version import Version

py_ver = Version("{0.major}.{0.minor}.{0.micro}".format(sys.version_info))
if py_ver not in SpecifierSet(PYTHON_REQUIRES):
raise SystemExit(
f"This version of pip does not support python {py_ver} "
f"(requires {PYTHON_REQUIRES})"
)


# TODO https:/pypa/pip/issues/11294
sys.meta_path.insert(0, PipImportRedirectingFinder())

assert __name__ == "__main__", "Cannot run __pip-runner__.py as a non-main module"
check_python_version()
runpy.run_module("pip", run_name="__main__", alter_sys=True)

0 comments on commit 3333891

Please sign in to comment.