uv icon indicating copy to clipboard operation
uv copied to clipboard

Add installation option for portable entry points

Open ofek opened this issue 1 year ago • 3 comments

I have a use case where I need to pre-build an entire installation which will be distributed to any number of machines. When installing packages that have entry points, the location becomes hardcoded to an absolute path.

Instead, this should be relative to the Python executable used for installation. For non-Windows systems the python-build-standalone project does this for pip for example:

#!/bin/sh
"exec" "$(dirname $0)/python3.12" "$0" "$@"
# -*- coding: utf-8 -*-
import re
import sys
from pip._internal.cli.main import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

The trampoline logic should be equivalently simple for Windows.

Maybe this should become the default in future?

ofek avatar May 20 '24 05:05 ofek

This definitely should not be the default. Entry point wrappers are built with absolute paths to the interpreter so that the wrapper can be copied or symlinked to other locations, without needing to copy the Python environment as well. This is something that's used by a lot of applications - most notably pipx, which symlinks ~/.local/bin/appname.exe to ~/.local/pipx/venvs/appenv/Scripts/appname.exe.

pfmoore avatar May 20 '24 14:05 pfmoore

Makes sense, thanks for that context @pfmoore.

charliermarsh avatar May 20 '24 14:05 charliermarsh

I have a similar use-case. It would be great to be able to skip patching wrappers post-install.

(On Windows-systems I am using <launcher_dir> for distlib-based launchers)

skoslowski avatar May 21 '24 14:05 skoslowski

FYI since https://github.com/astral-sh/uv/pull/3717 this is now completely possible to implement and I do so here:

  • https://github.com/pypa/hatch/blob/hatch-v1.11.1/release/windows/make_scripts_portable.py
  • https://github.com/pypa/hatch/blob/hatch-v1.11.1/release/unix/make_scripts_portable.py

ofek avatar May 23 '24 21:05 ofek

I'm confused as to what's happening here. I could test, but I have limited time right now so if someone can explain, I'd be grateful. If I uv pip install a project that has a script entry point, does that entry point by default still use an absolute path to the Python executable binary?

pfmoore avatar May 23 '24 21:05 pfmoore

Yeah. We didn’t change any behavior in uv itself, except we changed our Windows trampoline to accept a relative path (though we never write one to it in uv). Ofek is then post-processing the scripts in Hatch, I think, to rewrite the absolute paths as relative.

charliermarsh avatar May 23 '24 22:05 charliermarsh

Yes that is exactly correct and I can remove that entire logic once such a flag exists in UV! I posted those links mostly to assist in someone doing the implementation.

ofek avatar May 24 '24 01:05 ofek