fix(cookbook): skip pip --user fallback inside virtualenvs (#388) (#889)

The dependency-install fallback chain unconditionally ran
'pip install --user', which fails inside a virtualenv (and as root in
LXC/containers) with 'Can not perform a --user install. User site-packages
are not visible in this virtualenv.' — even though the function's docstring
already noted --user is invalid in venvs.

Guard the --user fallback with a venv check so it only runs outside a venv
(where --user is actually valid for PEP-668 system Pythons). Derive the venv
probe interpreter from the install command (python for 'pip', python3 for
'pip3'/'python3 -m pip') so the check runs in pip's own environment. System
PEP-668 installs keep the --user fallback; venv/LXC-root installs no longer
hit the --user error. Updated the unit test for the new chain.

Closes #388
This commit is contained in:
Tatlatat
2026-06-02 10:23:20 +07:00
committed by GitHub
parent 966b53df77
commit 9a1893760d
2 changed files with 18 additions and 5 deletions
+3 -2
View File
@@ -92,8 +92,9 @@ def test_pip_install_fallback_chain_prefers_venv_safe_install():
def test_pip_install_fallback_chain_allows_custom_python_command():
chain = _pip_install_fallback_chain("hf_transfer", python_cmd="pip", upgrade=False)
assert chain == (
"pip install -q hf_transfer 2>/dev/null || "
"pip install --user --break-system-packages -q hf_transfer 2>/dev/null"
'pip install -q hf_transfer 2>/dev/null || { '
'python -c "import sys; sys.exit(0 if sys.prefix != sys.base_prefix else 1)"'
' || pip install --user --break-system-packages -q hf_transfer 2>/dev/null; }'
)