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

Conversation

mcepl
Copy link
Contributor

@mcepl mcepl commented Jun 13, 2025

When cross-compiling, the local Python interpreter that is used to run ensurepip may not have the same value of sys.prefix as the value of the 'prefix' variable that is set in the Makefile.

With the following values used to install Python locally for a later copy to the files hierarchy owned by the 'termux' application on an Android device:

    DESTDIR=/tmp/android
    prefix=/data/data/com.termux/files/usr/local

'make install' causes ensurepip to install pip in $(DESTDIR)/usr/local instead of the expected $(DESTDIR)/$(prefix) where is installed the standard library.

The attached patch fixes the problem. The patch was implemented assuming that pip uses distutils for the installation (note that setup.py also uses the --prefix option in the Makefile), but I know nothing about pip so forgive me if the patch is wrong and please just assume it is just a way to demonstrate the problem.

Fixes: #75229
Co-authored-by: Pradyun Gedam pradyunsg@gmail.com
Co-authored-by: Erlend E. Aasland erlend.aasland@protonmail.com
Co-authored-by: Zackery Spytz zspytz@gmail.com
References: #17634
Signed-off-by: Matěj Cepl mcepl@cepl.eu

xdegaye and others added 2 commits June 13, 2025 12:03
When cross-compiling, the local Python interpreter that is used
to run `ensurepip` may not have the same value of `sys.prefix` as
the value of the 'prefix' variable that is set in the Makefile.

With the following values used to install Python locally for
a later copy to the files hierarchy owned by the 'termux'
application on an Android device:

    DESTDIR=/tmp/android
    prefix=/data/data/com.termux/files/usr/local

'make install' causes ensurepip to install pip in
$(DESTDIR)/usr/local instead of the expected $(DESTDIR)/$(prefix)
where is installed the standard library.

The attached patch fixes the problem. The patch was implemented
assuming that pip uses distutils for the installation (note that
setup.py also uses the --prefix option in the Makefile), but I
know nothing about pip so forgive me if the patch is wrong and
please just assume it is just a way to demonstrate the problem.

Fixes: python#75229
Fixes: https://bugs.python.org/issue31046
Co-authored-by: Pradyun Gedam <pradyunsg@gmail.com>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Co-authored-by: Zackery Spytz <zspytz@gmail.com>
References: python#17634
Signed-off-by: Matěj Cepl <mcepl@cepl.eu>
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: python#17634 (comment)
Signed-off-by: Matěj Cepl <mcepl@cepl.eu>
@pfmoore
Copy link
Member

pfmoore commented Jun 13, 2025

Has this PR addressed the various concerns raised in #17634 or is it simply a copy of that PR, reopened by a new author?

I'm concerned by the comment in the OP here:

I know nothing about pip so forgive me if the patch is wrong and please just assume it is just a way to demonstrate the problem.

I'm not sure who is expected to take responsibility for the correctness of this PR in the light of this statement?

@mcepl mcepl force-pushed the bpo-31046-ensurepip-prefix branch from 4b24401 to 5c4b65b Compare June 13, 2025 21:46
@mcepl
Copy link
Contributor Author

mcepl commented Jun 13, 2025 via email

Copy link

@JasonGantner JasonGantner left a comment

Choose a reason for hiding this comment

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

Thank you for the work improving this PR.

I've noticed two small issues, could you take them into consideration?

PS: the issue in Makefile.pre.in is the reason a lot of tests are failing

Comment on lines +173 to +180
# 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 = "/"

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.

@@ -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) ; \

@@ -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) ; \

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ensurepip does not honour the value of $(prefix)
4 participants