@@ -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