Reputation: 6408
From pip install --help
:
--user Install to the Python user install directory for your platform.
Typically ~/.local/, or %APPDATA%\Python on Windows.
(See the Python documentation for site.USER_BASE for full details.)
The documentation for site.USER_BASE
is a terrifying wormhole of interesting Unix-like subject matter that I don't understand.
What is the purpose of --user
in plain English? Why would installing the package to ~/.local/
matter? Why not just put an executable somewhere in my $PATH
?
Upvotes: 422
Views: 667862
Reputation: 908
On macOS, the reason for using the --user
flag is to make sure we don't corrupt the libraries the OS relies on. A conservative approach for many macOS users is to avoid installing or updating pip with a command that requires sudo
. Thus, this includes installing to /usr/local/bin
...
Reference: Installing Python for Neovim (Setting up Python for Neovim)
I'm not all clear why installing into /usr/local/bin
is a risk on a Mac, given the fact that the system only relies on Python binaries in /Library/Frameworks/
and /usr/bin
. I suspect it's because as noted above, installing into /usr/local/bin
requires sudo
which opens the door to making a costly mistake with the system libraries. Thus, installing into ~/.local/bin
is a sure fire way to avoid this risk.
Reference: Using Python on a Mac (4. Using Python on a Macintosh)
Finally, to the degree there is a benefit of installing packages into the /usr/local/bin
, does it make sense to change the owner of the directory from root
to user
? This would avoid having to use sudo
while still protecting against making system-dependent changes.* Is this a security default a relic of how Unix systems were more often used in the past (as servers)? Or at minimum, just a good way to go for Mac users not hosting a server?
*Note: Mac's System Integrity Protection (SIP) feature also seems to protect the user from changing the system-dependent libraries.
Upvotes: 11
Reputation: 1
If you are using your own Conda environment, don't use --user
. And if you are using a public Conda environment, use --user
to install packages at your home ~/.local/lib/
.
Upvotes: 0
Reputation: 1763
The best way is to install virtualenv and not require the --user
confusion. You will get more flexibility and not worry about clobbering the different Python versions and projects every time you pip install a package.
Upvotes: 16
Reputation: 7347
--user
installs in site.USER_SITE
.
For my case, it was /Users/.../Library/Python/2.7/bin
. So I have added that to my PATH (in ~/.bash_profile
file):
export PATH=$PATH:/Users/.../Library/Python/2.7/bin
Upvotes: 40
Reputation: 11639
pip <command> --user
changes the scope of the current pip command to work on the current user account's local python package install location, rather than the system-wide package install location, which is the default.
This only really matters on a multi-user machine. Anything installed to the system location will be visible to all users, so installing to the user location will keep that package installation separate from other users (they will not see it, and would have to install it themselves separately to use it). Because there can be version conflicts, installing a package with dependencies needed by other packages can cause problems, so it's best not to push all packages a given user uses to the system install location.
--user
location. It will be installed to a different folder, that may or may not need to be added to the path, depending on the package and how it's used (many packages install command-line tools that must be on the path to run from a shell).--user
is preferred to using root/sudo or requiring administrator installation and affecting the Python environment of every user, except in cases of general packages that the administrator wants to make available to all users by default.
apt
, rather than pip
.venv
command in the Python VENV docs.The --user
option in an active venv/virtualenv environment will install to the local user python location (same as without a virtual environment).
Packages are installed to the virtual environment by default, but if you use --user
it will force it to install outside the virtual environments, in the users python script directory (in Windows, this currently is c:\users\<username>\appdata\roaming\python\python37\scripts
for me with Python 3.7).
However, you won't be able to access a system or user install from within virtual environment (even if you used --user
while in a virtual environment).
If you install a virtual environment with the --system-site-packages
argument, you will have access to the system script folder for python. I believe this included the user python script folder as well, but I'm unsure. However, there may be unintended consequences for this and it is not the intended way to use virtual environments.
You can find the location of the user install folder for python with python -m site --user-base
. I'm finding conflicting information in Q&A's, the documentation and actually using this command on my PC as to what the defaults are, but they are underneath the user home directory (~
shortcut in *nix, and c:\users\<username>
typically for Windows).
The --user
option is not a valid for every command. For example pip uninstall
will find and uninstall packages wherever they were installed (in the user folder, virtual environment folder, etc.) and the --user
option is not valid.
Things installed with pip install --user
will be installed in a local location that will only be seen by the current user account, and will not require root access (on *nix) or administrator access (on Windows).
The --user
option modifies all pip
commands that accept it to see/operate on the user install folder, so if you use pip list --user
it will only show you packages installed with pip install --user
.
Upvotes: 40
Reputation: 2201
Why would intalling the package to ~/.local/ matter? Why not just put an executable somewhere in my $PATH?
~/.local/bin directory
is theoretically expected to be in your $PATH
.
According to these people it's a bug not adding it in the $PATH
when using systemd
.
This answer explains it more extensively.
But even if your distro includes the ~/.local/bin
directory to the $PATH
, it might be in the following form (inside ~/.profile
):
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi
which would require you to logout and login again, the first time the directory is created.
Upvotes: 1
Reputation: 17478
Just a warning:
According to this issue, --user
is currently not valid inside a virtual env's pip
, since a user location doesn't really make sense for a virtual environment.
So do not use pip install --user some_pkg
inside a virtual environment, otherwise, virtual environment's pip
will be confused. See this answer for more details.
Upvotes: 61
Reputation: 4329
Other answers mention site.USER_SITE
as where Python packages get placed. If you're looking for binaries, these go in {site.USER_BASE}/bin
.
If you want to add this directory to your shell's search path, use:
export PATH="${PATH}:$(python3 -c 'import site; print(site.USER_BASE)')/bin"
Upvotes: 30
Reputation:
pip defaults to installing Python packages to a system directory (such as /usr/local/lib/python3.4
). This requires root access.
--user
makes pip install packages in your home directory instead, which doesn't require any special privileges.
Upvotes: 468