diff --git a/setup.py b/setup.py index 9b7fdeb1134..2179d34d2bf 100644 --- a/setup.py +++ b/setup.py @@ -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", ) diff --git a/src/pip/__pip-runner__.py b/src/pip/__pip-runner__.py index 280e99f2f08..b83a5664e09 100644 --- a/src/pip/__pip-runner__.py +++ b/src/pip/__pip-runner__.py @@ -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: @@ -30,8 +32,22 @@ def find_spec( return spec +def check_python_version(): + # Import here to ensure the imports happen after the sys.meta_path change. + from pip._vendor.packaging.version import Version + from pip._vendor.packaging.specifiers import SpecifierSet + + 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://github.com/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)