From f88420319db450aefbed1500f04e31be46874aaf Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Tue, 27 Apr 2021 14:27:42 +0800 Subject: [PATCH] Fallback to self-invoke via directory on 3.6 This fixes a compatibility issue when a PEP 517 build requirement itself needs to be built in an isolated environment, caused by importlib.resources not being available. --- news/9878.bugfix.rst | 2 ++ src/pip/_internal/build_env.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 news/9878.bugfix.rst diff --git a/news/9878.bugfix.rst b/news/9878.bugfix.rst new file mode 100644 index 00000000000..8fb1306f3ec --- /dev/null +++ b/news/9878.bugfix.rst @@ -0,0 +1,2 @@ +Fix Python 3.6 compatibility when a PEP 517 build requirement itself needs to be +built in an isolated environment. diff --git a/src/pip/_internal/build_env.py b/src/pip/_internal/build_env.py index 755dbb1f712..cdf04324107 100644 --- a/src/pip/_internal/build_env.py +++ b/src/pip/_internal/build_env.py @@ -187,9 +187,17 @@ def install_requirements( prefix.setup = True if not requirements: return - with _create_standalone_pip() as standalone_pip: + with contextlib.ExitStack() as ctx: + # TODO: Remove this block when dropping 3.6 support. Python 3.6 + # lacks importlib.resources and pep517 has issues loading files in + # a zip, so we fallback to the "old" method by adding the current + # pip directory to the child process's sys.path. + if sys.version_info < (3, 7): + pip_runnable = os.path.dirname(pip_location) + else: + pip_runnable = ctx.enter_context(_create_standalone_pip()) self._install_requirements( - standalone_pip, + pip_runnable, finder, requirements, prefix, @@ -198,14 +206,14 @@ def install_requirements( @staticmethod def _install_requirements( - standalone_pip: str, + pip_runnable: str, finder: "PackageFinder", requirements: Iterable[str], prefix: _Prefix, message: str, ) -> None: args = [ - sys.executable, standalone_pip, 'install', + sys.executable, pip_runnable, 'install', '--ignore-installed', '--no-user', '--prefix', prefix.path, '--no-warn-script-location', ] # type: List[str]