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 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bpo-31046: Fix ensurepip script shebangs with --root
When using `python -m ensurepip` with the `--root` option for staged
installations, the generated pip script contained an incorrect shebang
that pointed into the staging directory. This made the installation
unusable once the staging directory was removed.

This commit fixes the issue by using the internal pip `--executable`
option to force the shebang to point to the correct, final
interpreter path.

It also corrects related pathing issues:

- Removes the check that incorrectly disallowed using --root and
  --prefix together.
- Defaults the installation prefix to `/` when --root is used alone,
  ensuring installation occurs at the base of the staging directory.

References: #17634 (comment)
Signed-off-by: Matěj Cepl <mcepl@cepl.eu>
  • Loading branch information
mcepl committed Jul 25, 2025
commit 0940129a44c57675fafd44660636dd5225f5e243
35 changes: 27 additions & 8 deletions Lib/ensurepip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ def _bootstrap(*, root=None, upgrade=False, user=False,

Note that calling this function will alter both sys.path and os.environ.
"""
if root is not None and prefix is not None:
raise ValueError("Cannot use 'root' and 'prefix' together")
if altinstall and default_pip:
raise ValueError("Cannot use altinstall and default_pip together")

Expand Down Expand Up @@ -162,17 +160,38 @@ 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 prefix:
args += ["--prefix", prefix]
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 = "/"

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

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

                # Force the script shebang to point to the correct, final
                # executable path. This is necessary when --root is used.
                executable_path = (
                    Path(effective_prefix) / "bin" / Path(sys.executable).name
                )
                args += ["--executable", os.fsdecode(executable_path)]

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


Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy