Skip to content

Commit cd352a0

Browse files
committed
Check python version in __pip-runner__.py
1 parent ddfa05c commit cd352a0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,7 @@ def get_version(rel_path: str) -> str:
8181
],
8282
},
8383
zip_safe=False,
84+
# NOTE: python_requires is duplicated in __pip-runner__.py.
85+
# When changing this value, please change the other copy as well.
8486
python_requires=">=3.7",
8587
)

src/pip/__pip-runner__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from typing import Optional, Sequence, Union
1313

1414
PIP_SOURCES_ROOT = dirname(dirname(__file__))
15+
# Copied from setup.py
16+
PYTHON_REQUIRES = ">=3.7"
1517

1618

1719
class PipImportRedirectingFinder:
@@ -30,8 +32,22 @@ def find_spec(
3032
return spec
3133

3234

35+
def check_python_version():
36+
# Import here to ensure the imports happen after the sys.meta_path change.
37+
from pip._vendor.packaging.version import Version
38+
from pip._vendor.packaging.specifiers import SpecifierSet
39+
40+
py_ver = Version("{0.major}.{0.minor}.{0.micro}".format(sys.version_info))
41+
if py_ver not in SpecifierSet(PYTHON_REQUIRES):
42+
raise SystemExit(
43+
f"This version of pip does not support python {py_ver} "
44+
f"(requires {PYTHON_REQUIRES})"
45+
)
46+
47+
3348
# TODO https://github.com/pypa/pip/issues/11294
3449
sys.meta_path.insert(0, PipImportRedirectingFinder())
3550

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

0 commit comments

Comments
 (0)