Skip to content

Commit 88f82b2

Browse files
hroncokpradyunsg
andauthored
bpo-38662: ensurepip invokes pip via runpy (GH-18901)
The ensurepip module now invokes pip via the runpy module. Hence it is no longer tightly coupled with the internal API of the bundled pip version, allowing easier updates to a newer pip version both internally and for distributors. This way, any changes to the internal pip API won't mean ensurepip needs to be changed as well. Also, distributors can update their pip wheels independent on CPython release schedule. Co-Authored-By: Pradyun Gedam <[email protected]> Co-Authored-By: Miro Hrončok <[email protected]>
1 parent d06eec2 commit 88f82b2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Lib/ensurepip/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import os.path
33
import sys
4+
import runpy
45
import tempfile
56
from importlib import resources
67

@@ -26,9 +27,18 @@ def _run_pip(args, additional_paths=None):
2627
if additional_paths is not None:
2728
sys.path = additional_paths + sys.path
2829

29-
# Install the bundled software
30-
import pip._internal
31-
return pip._internal.main(args)
30+
# Invoke pip as if it's the main module, and catch the exit.
31+
backup_argv = sys.argv[:]
32+
sys.argv[1:] = args
33+
try:
34+
# run_module() alters sys.modules and sys.argv, but restores them at exit
35+
runpy.run_module("pip", run_name="__main__", alter_sys=True)
36+
except SystemExit as exc:
37+
return exc.code
38+
finally:
39+
sys.argv[:] = backup_argv
40+
41+
raise SystemError("pip did not exit, this should never happen")
3242

3343

3444
def version():
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The ``ensurepip`` module now invokes ``pip`` via the ``runpy`` module.
2+
Hence it is no longer tightly coupled with the internal API of the bundled
3+
``pip`` version, allowing easier updates to a newer ``pip`` version both
4+
internally and for distributors.

0 commit comments

Comments
 (0)