Skip to content

Commit f8008ba

Browse files
pradyunsghroncok
andcommitted
bpo-38662: Invoke pip via runpy, in ensurepip
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 <pradyunsg@gmail.com> Co-Authored-By: Miro Hrončok <miro@hroncok.cz>
1 parent e5e5632 commit f8008ba

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Lib/ensurepip/__init__.py

Lines changed: 12 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,17 @@ 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+
runpy.run_module("pip", run_name="__main__", alter_sys=True)
35+
except SystemExit as e:
36+
return e.code
37+
finally:
38+
sys.argv[:] = backup_argv
39+
40+
raise SystemError("pip have not exited, that should never happen")
3241

3342

3443
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)