Skip to content

Commit 990ca8a

Browse files
authored
Merge pull request #8796 from pelson/honour_user_site
Disallow --user installs if site.ENABLE_USER_SITE is False.
2 parents 2a2960d + ed399df commit 990ca8a

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

news/8794.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent --user installs if site.ENABLE_USER_SITE is set to False.

src/pip/_internal/commands/install.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ def decide_user_install(
687687
logger.debug("Non-user install by explicit request")
688688
return False
689689

690+
# If we have been asked for a user install explicitly, check compatibility.
690691
if use_user_site:
691692
if prefix_path:
692693
raise CommandError(
@@ -698,6 +699,13 @@ def decide_user_install(
698699
"Can not perform a '--user' install. User site-packages "
699700
"are not visible in this virtualenv."
700701
)
702+
# Catch all remaining cases which honour the site.ENABLE_USER_SITE
703+
# value, such as a plain Python installation (e.g. no virtualenv).
704+
if not site.ENABLE_USER_SITE:
705+
raise InstallationError(
706+
"Can not perform a '--user' install. User site-packages "
707+
"are disabled for this Python."
708+
)
701709
logger.debug("User install by explicit request")
702710
return True
703711

tests/functional/test_install_user.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,51 @@ def test_install_user_in_global_virtualenv_with_conflict_fails(
328328
f"Will not install to the user site because it will lack sys.path "
329329
f"precedence to pkg in {dist_location}"
330330
) in result2.stderr
331+
332+
def test_install_user_nositepkgs_fails(
333+
self,
334+
script: PipTestEnvironment,
335+
data: TestData,
336+
) -> None:
337+
"""
338+
Test that --user install fails when user site-packages are disabled.
339+
"""
340+
create_basic_wheel_for_package(script, "pkg", "0.1")
341+
342+
# Create a custom Python script that disables user site and runs pip via exec
343+
test_script = script.scratch_path / "test_disable_user_site.py"
344+
test_script.write_text(
345+
textwrap.dedent(
346+
f"""
347+
import site
348+
import sys
349+
350+
# Make sys.base_prefix equal to sys.prefix to simulate not being in a venv
351+
# This ensures virtualenv_no_global() returns False, so we test the
352+
# site.ENABLE_USER_SITE path
353+
sys.base_prefix = sys.prefix
354+
site.ENABLE_USER_SITE = False
355+
356+
# Set up sys.argv to simulate running pip install --user
357+
sys.argv = [
358+
"pip", "install",
359+
"--no-cache-dir",
360+
"--no-index",
361+
"--find-links",
362+
r"{script.scratch_path}",
363+
"pkg",
364+
"--user"
365+
]
366+
367+
# Import and run pip's main
368+
from pip._internal.cli.main import main
369+
sys.exit(main())
370+
"""
371+
)
372+
)
373+
374+
result = script.run("python", str(test_script), expect_error=True)
375+
assert (
376+
"Can not perform a '--user' install. User site-packages are "
377+
"disabled for this Python." in result.stderr
378+
)

0 commit comments

Comments
 (0)