Skip to content

Commit 3ee7cb2

Browse files
ZackerySpytzxdegaye
authored andcommitted
bpo-31046: ensurepip does not honour the value of $(prefix)
Co-Authored-By: Xavier de Gaye <[email protected]>
1 parent 747d390 commit 3ee7cb2

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

Doc/library/ensurepip.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ is at least as recent as the one available in ``ensurepip``, pass the
6161
By default, ``pip`` is installed into the current virtual environment
6262
(if one is active) or into the system site packages (if there is no
6363
active virtual environment). The installation location can be controlled
64-
through two additional command line options:
64+
through some additional command line options:
65+
66+
.. option:: --prefix <dir>
67+
68+
Installs ``pip`` using the given directory prefix.
6569

6670
.. option:: --root <dir>
6771

@@ -102,7 +106,7 @@ Module API
102106
Returns a string specifying the available version of pip that will be
103107
installed when bootstrapping an environment.
104108

105-
.. function:: bootstrap(root=None, upgrade=False, user=False, \
109+
.. function:: bootstrap(root=None, prefix=None, upgrade=False, user=False, \
106110
altinstall=False, default_pip=False, \
107111
verbosity=0)
108112

@@ -112,6 +116,8 @@ Module API
112116
If *root* is ``None``, then installation uses the default install location
113117
for the current environment.
114118

119+
*prefix* specifies the directory prefix to use when installing.
120+
115121
*upgrade* indicates whether or not to upgrade an existing installation
116122
of an earlier version of ``pip`` to the available version.
117123

@@ -132,6 +138,8 @@ Module API
132138
*verbosity* controls the level of output to :data:`sys.stdout` from the
133139
bootstrapping operation.
134140

141+
.. versionchanged:: 3.9 the *prefix* parameter was added.
142+
135143
.. audit-event:: ensurepip.bootstrap root ensurepip.bootstrap
136144

137145
.. note::

Lib/ensurepip/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,27 @@ def _disable_pip_configuration_settings():
106106
os.environ['PIP_CONFIG_FILE'] = os.devnull
107107

108108

109-
def bootstrap(*, root=None, upgrade=False, user=False,
109+
def bootstrap(*, root=None, prefix=None, upgrade=False, user=False,
110110
altinstall=False, default_pip=False,
111111
verbosity=0):
112112
"""
113113
Bootstrap pip into the current Python installation (or the given root
114-
directory).
114+
and directory prefix).
115115
116116
Note that calling this function will alter both sys.path and os.environ.
117117
"""
118118
# Discard the return value
119-
_bootstrap(root=root, upgrade=upgrade, user=user,
119+
_bootstrap(root=root, prefix=prefix, upgrade=upgrade, user=user,
120120
altinstall=altinstall, default_pip=default_pip,
121121
verbosity=verbosity)
122122

123123

124-
def _bootstrap(*, root=None, upgrade=False, user=False,
124+
def _bootstrap(*, root=None, prefix=None, upgrade=False, user=False,
125125
altinstall=False, default_pip=False,
126126
verbosity=0):
127127
"""
128128
Bootstrap pip into the current Python installation (or the given root
129-
directory). Returns pip command status code.
129+
and directory prefix). Returns pip command status code.
130130
131131
Note that calling this function will alter both sys.path and os.environ.
132132
"""
@@ -162,6 +162,8 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
162162
args = ["install", "--no-cache-dir", "--no-index", "--find-links", tmpdir]
163163
if root:
164164
args += ["--root", root]
165+
if prefix:
166+
args += ["--prefix", prefix]
165167
if upgrade:
166168
args += ["--upgrade"]
167169
if user:
@@ -237,6 +239,11 @@ def _main(argv=None):
237239
default=None,
238240
help="Install everything relative to this alternate root directory.",
239241
)
242+
parser.add_argument(
243+
"--prefix",
244+
default=None,
245+
help="Install everything using this prefix.",
246+
)
240247
parser.add_argument(
241248
"--altinstall",
242249
action="store_true",
@@ -256,6 +263,7 @@ def _main(argv=None):
256263

257264
return _bootstrap(
258265
root=args.root,
266+
prefix=args.prefix,
259267
upgrade=args.upgrade,
260268
user=args.user,
261269
verbosity=args.verbosity,

Lib/test/test_ensurepip.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ def test_bootstrapping_with_root(self):
100100
unittest.mock.ANY,
101101
)
102102

103+
def test_bootstrapping_with_prefix(self):
104+
ensurepip.bootstrap(prefix="/foo/bar/")
105+
self.run_pip.assert_called_once_with(
106+
[
107+
"install", "--no-index", "--find-links",
108+
unittest.mock.ANY, "--prefix", "/foo/bar/",
109+
"setuptools", "pip",
110+
],
111+
unittest.mock.ANY,
112+
)
113+
103114
def test_bootstrapping_with_user(self):
104115
ensurepip.bootstrap(user=True)
105116

Makefile.pre.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ install: @FRAMEWORKINSTALLFIRST@ @INSTALLTARGETS@ @FRAMEWORKINSTALLLAST@
23362336
install|*) ensurepip="" ;; \
23372337
esac; \
23382338
$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
2339-
$$ensurepip --root=$(DESTDIR)/ ; \
2339+
$$ensurepip --root=$(DESTDIR)/ --prefix=$(prefix) ; \
23402340
fi
23412341

23422342
.PHONY: altinstall
@@ -2347,7 +2347,7 @@ altinstall: commoninstall
23472347
install|*) ensurepip="--altinstall" ;; \
23482348
esac; \
23492349
$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
2350-
$$ensurepip --root=$(DESTDIR)/ ; \
2350+
$$ensurepip --root=$(DESTDIR)/ --prefix=$(prefix) ; \
23512351
fi
23522352

23532353
.PHONY: commoninstall
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A directory prefix can now be specified when using :mod:`ensurepip`.

0 commit comments

Comments
 (0)