Skip to content

Commit 30dae2b

Browse files
committed
docs: Improve --user/--no-user documentation (#12924)
1 parent 2d04a35 commit 30dae2b

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

docs/html/user_guide.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,92 @@ To install "SomePackage" into an environment with ``site.USER_BASE`` customized
897897
set PYTHONUSERBASE=c:/myappenv
898898
py -m pip install --user SomePackage
899899
900+
Automatic User Install Fallback
901+
-------------------------------
902+
903+
.. important::
904+
905+
When pip detects that the normal ``site-packages`` directory is not
906+
writeable (e.g., when running as a non-root user on a system Python),
907+
it will **automatically default to a user installation** without requiring
908+
the ``--user`` flag. You will see this message:
909+
910+
.. code-block:: text
911+
912+
Defaulting to user installation because normal site-packages is not writeable
913+
914+
This automatic fallback can be surprising, especially if the warning is
915+
missed among other output. If you explicitly want to prevent this behavior
916+
and instead receive an error when ``site-packages`` is not writeable, use
917+
the ``--no-user`` option.
918+
919+
The ``--no-user`` Option
920+
------------------------
921+
922+
The ``--no-user`` option explicitly disables user installs, ensuring that pip
923+
will attempt to install to the system ``site-packages`` directory. If pip
924+
cannot write to ``site-packages``, it will fail with a permission error rather
925+
than silently falling back to a user install.
926+
927+
.. tab:: Unix/macOS
928+
929+
.. code-block:: shell
930+
931+
python -m pip install --no-user SomePackage
932+
933+
.. tab:: Windows
934+
935+
.. code-block:: shell
936+
937+
py -m pip install --no-user SomePackage
938+
939+
This is useful in scenarios where:
940+
941+
- You want to ensure packages are installed globally and not in user space.
942+
- You want pip to fail loudly if it doesn't have the necessary permissions,
943+
rather than silently succeeding with a user install.
944+
- You are debugging installation issues and want to understand exactly where
945+
packages are being installed.
946+
947+
Environment Variables
948+
---------------------
949+
950+
The user install behavior can also be controlled via environment variables:
951+
952+
``PIP_USER``
953+
Set to ``1``, ``true``, or ``yes`` to enable user installs by default
954+
(equivalent to ``--user``). Set to ``0``, ``false``, or ``no`` to disable
955+
the automatic user install fallback (equivalent to ``--no-user``).
956+
957+
.. tab:: Unix/macOS
958+
959+
.. code-block:: shell
960+
961+
# Always use user installs
962+
export PIP_USER=1
963+
python -m pip install SomePackage
964+
965+
# Never fall back to user installs
966+
export PIP_USER=0
967+
python -m pip install SomePackage
968+
969+
.. tab:: Windows
970+
971+
.. code-block:: shell
972+
973+
:: Always use user installs
974+
set PIP_USER=1
975+
py -m pip install SomePackage
976+
977+
:: Never fall back to user installs
978+
set PIP_USER=0
979+
py -m pip install SomePackage
980+
981+
``PYTHONUSERBASE``
982+
Customizes the user install location by changing the value of
983+
``site.USER_BASE``.
984+
985+
900986
``pip install --user`` follows four rules:
901987

902988
#. When globally installed packages are on the python path, and they *conflict*

news/12924.doc.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Improved documentation for the ``--user`` and ``--no-user`` options, including:
2+
3+
- Documented the automatic user installation fallback behavior that occurs when site-packages is not writeable.
4+
- Made the ``--no-user`` option visible in help output (previously hidden).
5+
- Documented the ``PIP_USER`` environment variable for controlling user install behavior.
6+
- Added examples and use cases for when to use ``--no-user``.

src/pip/_internal/commands/install.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,13 @@ def add_options(self) -> None:
135135
"--no-user",
136136
dest="use_user_site",
137137
action="store_false",
138-
help=SUPPRESS_HELP,
138+
help=(
139+
"Disable user site-packages install. If site-packages is not "
140+
"writeable, pip will fail with a permission error rather than "
141+
"falling back to a user install."
142+
),
139143
)
144+
140145
self.cmd_opts.add_option(
141146
"--root",
142147
dest="root_path",

0 commit comments

Comments
 (0)