Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of pip's --user option on Windows?

Tags:

python

pip

I know that when I install Python packages on Linux I need to use the --user option of pip to install the package in my home directory, otherwise I'll need root privilege. But on Windows it seems that whether or not I use this option, the package will always be installed in my home directory (C:\Users\{username}\...), but the specific path will be slightly different. With the --user option, the package will be installed to c:\users\{username}\appdata\roaming\python\python37\site-packages, while without this option the package will be installed to c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages.

C:\>pip3 install pyyaml
Collecting pyyaml
  Using cached https://files.pythonhosted.org/packages/45/19/53c041b8719eaf88ce1cdb51bea1c5a2844433e79c23a2a8aeeaa0e27fd8/PyYAML-5.1.1-cp37-cp37m-win32.whl
Installing collected packages: pyyaml
Successfully installed pyyaml-5.1.1

C:\>pip3 show pyyaml
Name: PyYAML
Version: 5.1.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: [email protected]
License: MIT
Location: c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages
Requires:
Required-by:

C:\>pip3 uninstall pyyaml
Uninstalling PyYAML-5.1.1:
  Would remove:
    c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\_yaml.cp37-win32.pyd
    c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\pyyaml-5.1.1.dist-info\*
    c:\users\{username}\appdata\local\programs\python\python37-32\lib\site-packages\yaml\*
Proceed (y/n)? y
  Successfully uninstalled PyYAML-5.1.1

C:\>pip3 install --user pyyaml
Collecting pyyaml
  Using cached https://files.pythonhosted.org/packages/45/19/53c041b8719eaf88ce1cdb51bea1c5a2844433e79c23a2a8aeeaa0e27fd8/PyYAML-5.1.1-cp37-cp37m-win32.whl
Installing collected packages: pyyaml
Successfully installed pyyaml-5.1.1

C:\>pip3 show pyyaml
Name: PyYAML
Version: 5.1.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: [email protected]
License: MIT
Location: c:\users\{username}\appdata\roaming\python\python37\site-packages
Requires:
Required-by:

All the commands were run under a normal user, not Administrator.

So what is the use of pip's --user option on Windows? Is it necessary to use --user option whenever I install packages?

like image 829
wtz Avatar asked Nov 06 '22 16:11

wtz


1 Answers

It is indeed confusing how the Windows Python installer handles install location with default settings (which you most probably used).

According to the documentation, when you run the installer and just click "Install Now":

  • You will not need to be an administrator (unless a system update for the C Runtime Library is required or you install the Python Launcher for Windows for all users)
  • Python will be installed into your user directory
  • The Python Launcher for Windows will be installed according to the option at the bottom of the first page.

Now, the option for the Python Launcher is also selected by default. This means, if your user account is in the "Administrator" group (which it typically is), Python (python.exe) will be installed in your %LocalAppData% directory (just as you have observed). However, the installer will still present you with a UAC prompt to confirm you have admin privileges, as it also wants to install the Python Launcher (py.exe). You end up with a local, "just for me" Python installation in your user directory, though for some intangible reason the Launcher is installed "for all users".

If you really want to have a system-wide Python installation, you need to select "Customize installation" on the first screen of the installer, then click "Next", and check "Install for all users" — which is not checked otherwise. The install location will then default to your %ProgramFiles% directory, usually C:\Program Files.

When you do user-installs with pip, it will put the packages in your %AppData% directory, which is AppData\Roaming in your user profile (as you have also observed). This is so that when you have a "roaming" account on a domain network, your personally installed packages follow you around, no matter from which computer on the network you log in. Obviously, that computer would have to have a system-wide Python installation "for all users" — of the Python interpreter, not the nearly irrelevant Python Launcher. This is where the default behavior, described above, makes absolutely zero sense, as you wouldn't be able to run the Python interpreter installed locally in some other user's profile when you log on to their computer.

On top of that, if you actually do use your profile to "roam" the domain network, all those --user packages, which, more likely than not, comprise thousands of files, will slow down the log-in process: every one of those files has to be sync'ed between domain storage and the local computer.

Bottom line: If you want to set this up properly, customize your installation to make sure it's installed in some directory that is in fact accessible to all users. Personally, I like to put it in C:\programs\Python, as I can then pip install anything for everyone and don't even need an elevated prompt — which one would for writing to C:\Program Files. Then again, requiring an elevated prompt may be advisable, depending on the circumstances.

like image 97
john-hen Avatar answered Nov 15 '22 11:11

john-hen