Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-75229: make ensurepip honour value of --prefix option #135488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Doc/library/ensurepip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ is at least as recent as the one available in ``ensurepip``, pass the
By default, ``pip`` is installed into the current virtual environment
(if one is active) or into the system site packages (if there is no
active virtual environment). The installation location can be controlled
through two additional command line options:
through some additional command line options:

.. option:: --prefix <dir>

Installs ``pip`` using the given directory prefix.

.. option:: --root <dir>

Expand Down Expand Up @@ -104,7 +108,7 @@ Module API

.. function:: bootstrap(root=None, upgrade=False, user=False, \
altinstall=False, default_pip=False, \
verbosity=0)
verbosity=0, prefix=None)

Bootstraps ``pip`` into the current or designated environment.

Expand Down Expand Up @@ -132,6 +136,12 @@ Module API
*verbosity* controls the level of output to :data:`sys.stdout` from the
bootstrapping operation.

*prefix* specifies the directory prefix to use when installing.

.. versionadded:: 3.14

The *prefix* parameter.

.. audit-event:: ensurepip.bootstrap root ensurepip.bootstrap

.. note::
Expand Down
40 changes: 31 additions & 9 deletions Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,25 @@ def _disable_pip_configuration_settings():

def bootstrap(*, root=None, upgrade=False, user=False,
altinstall=False, default_pip=False,
verbosity=0):
verbosity=0, prefix=None):
"""
Bootstrap pip into the current Python installation (or the given root
directory).
and directory prefix).

Note that calling this function will alter both sys.path and os.environ.
"""
# Discard the return value
_bootstrap(root=root, upgrade=upgrade, user=user,
altinstall=altinstall, default_pip=default_pip,
verbosity=verbosity)
verbosity=verbosity, prefix=prefix)


def _bootstrap(*, root=None, upgrade=False, user=False,
altinstall=False, default_pip=False,
verbosity=0):
verbosity=0, prefix=None):
"""
Bootstrap pip into the current Python installation (or the given root
directory). Returns pip command status code.
and directory prefix). Returns pip command status code.

Note that calling this function will alter both sys.path and os.environ.
"""
Expand Down Expand Up @@ -160,15 +160,31 @@ def _bootstrap(*, root=None, upgrade=False, user=False,

# Construct the arguments to be passed to the pip command
args = ["install", "--no-cache-dir", "--no-index", "--find-links", tmpdir]
if root:
args += ["--root", root]
if upgrade:
args += ["--upgrade"]
if user:
args += ["--user"]
if verbosity:
args += ["-" + "v" * verbosity]

if user:
# --user is mutually exclusive with --root/--prefix,
# pip will enforce this.
args += ["--user"]
else:
# Handle installation paths.
# If --root is given but not --prefix, we default to a prefix of "/"
# so that the install happens at the root of the --root directory.
# Otherwise, pip would use the configured sys.prefix, e.g.
# /usr/local, and install into ${root}/usr/local/.
effective_prefix = prefix
if root and not prefix:
effective_prefix = "/"
Comment on lines +173 to +180

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usual use cases for ensurepip are:

  • installing pip on the current(host) system -> sys.prefix is the right prefix to use
  • installing pip in a packaging directory -> the Makefile always provides the prefix (given Makefile.pre.in is patched correctly by this PR)

Adding this override of the default prefix would break the first use case without clear benefit to the second.


if root:
args += ["--root", root]

if effective_prefix:
args += ["--prefix", effective_prefix]

return _run_pip([*args, "pip"], [os.fsdecode(tmp_wheel_path)])


Expand Down Expand Up @@ -237,6 +253,11 @@ def _main(argv=None):
default=None,
help="Install everything relative to this alternate root directory.",
)
parser.add_argument(
"--prefix",
default=None,
help="Install everything using this prefix.",
)
parser.add_argument(
"--altinstall",
action="store_true",
Expand All @@ -256,6 +277,7 @@ def _main(argv=None):

return _bootstrap(
root=args.root,
prefix=args.prefix,
upgrade=args.upgrade,
user=args.user,
verbosity=args.verbosity,
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_ensurepip.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ def test_bootstrapping_with_root(self):
unittest.mock.ANY,
)

def test_bootstrapping_with_prefix(self):
ensurepip.bootstrap(prefix="/foo/bar/")
self.run_pip.assert_called_once_with(
[
"install", "--no-cache-dir", "--no-index", "--find-links",
unittest.mock.ANY, "--prefix", "/foo/bar/", "pip",
],
unittest.mock.ANY,
)

def test_root_and_prefix_mutual_exclusive(self):
with self.assertRaises(ValueError):
ensurepip.bootstrap(root="", prefix="")
self.assertFalse(self.run_pip.called)

def test_bootstrapping_with_user(self):
ensurepip.bootstrap(user=True)

Expand Down
4 changes: 2 additions & 2 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ install: @FRAMEWORKINSTALLFIRST@ @INSTALLTARGETS@ @FRAMEWORKINSTALLLAST@
install|*) ensurepip="" ;; \
esac; \
$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
$$ensurepip --root=$(DESTDIR)/ ; \
$$ensurepip --prefix=$(prefix) ; \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must preserve the --root argument here.

Suggested change
$$ensurepip --prefix=$(prefix) ; \
$$ensurepip --root=$(DESTDIR)/ --prefix=$(prefix) ; \

fi

.PHONY: altinstall
Expand All @@ -2347,7 +2347,7 @@ altinstall: commoninstall
install|*) ensurepip="--altinstall" ;; \
esac; \
$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
$$ensurepip --root=$(DESTDIR)/ ; \
$$ensurepip --prefix=$(prefix) ; \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also here

Suggested change
$$ensurepip --prefix=$(prefix) ; \
$$ensurepip --root=$(DESTDIR)/ --prefix=$(prefix) ; \

fi

.PHONY: commoninstall
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A directory prefix can now be specified when using :mod:`ensurepip`.
Loading