From 4681d386aebac109fe490eb00706b97290908d0a Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Fri, 1 Nov 2019 22:43:53 +0530 Subject: [PATCH] Invoke pip via runpy, in ensurepip --- Lib/ensurepip/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 386ed6c25c763e..eb3612551cd50f 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -1,6 +1,7 @@ import os import os.path import sys +import runpy import tempfile from importlib import resources @@ -26,9 +27,17 @@ def _run_pip(args, additional_paths=None): if additional_paths is not None: sys.path = additional_paths + sys.path - # Install the bundled software - import pip._internal - return pip._internal.main(args) + # Invoke pip as if it's the main module, and catch the exit. + backup_argv = sys.argv[:] + sys.argv[1:] = args + try: + runpy.run_module("pip", run_name="__main__", alter_sys=True) + except SystemExit as e: + return e.code + finally: + sys.argv[:] = backup_argv + + assert 0, "should never reach here" def version():