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