From 92e8866f13e168ce5043e40c3489561c7dce45d4 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 4 Jun 2026 00:05:36 +0000 Subject: [PATCH] pip_user_install: capability probe instead of version check Probe for --break-system-packages support once (pip --help, cached in _PIP_HAS_BSP) rather than comparing Ubuntu version numbers. Works on any pip >= 22.3 regardless of distro; older pip (Ubuntu 22.04, pip 22.0) falls back to --user only, which is correct there since PEP 668 isn't enforced on 22.04 anyway. The flag name is scary but harmless with --user: installs go to ~/.local/ which apt never manages regardless. https://claude.ai/code/session_01Y4dMKtkqkpvmgDKoRdzhTG --- lib/common.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/common.sh b/lib/common.sh index 0b44ba5..7ae864c 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -124,11 +124,21 @@ ubuntu_version_ge() { } # pip install --user as actual user. -# --break-system-packages (pip 22.3+) is required on Ubuntu 24.04+ where PEP 668 -# marks the system Python as externally managed; --user alone is not always enough. +# --break-system-packages overrides PEP 668 ("externally managed environment"), +# required on Ubuntu 24.04+ — the flag name sounds alarming but with --user the +# install goes to ~/.local/ which apt never touches; nothing system-level is at risk. +# The flag was added in pip 22.3; probe once so older pip (Ubuntu 22.04) still works. +_PIP_HAS_BSP="" +_pip_probe() { + [ -n "$_PIP_HAS_BSP" ] && return + pip3 install --help 2>/dev/null | grep -q -- '--break-system-packages' \ + && _PIP_HAS_BSP=1 || _PIP_HAS_BSP=0 +} + pip_user_install() { + _pip_probe local flags="--user --quiet" - ubuntu_version_ge "24.04" && flags="$flags --break-system-packages" + [ "$_PIP_HAS_BSP" = "1" ] && flags="$flags --break-system-packages" sudo -u "$ACTUAL_USER" pip3 install $flags "$@" }