Why Use `pip install — user`?

Kirill Bobrov
4 min readDec 3, 2024

When working with Python, you’re likely familiar with installing packages using the popular package manager, pip. It's a powerful tool that simplifies the process of adding third-party libraries to your Python environment. But you might have noticed the --user flag while installing something and wondered what its purpose is and why it might matter. In this post, we'll demystify pip install --user and explore its significance in plain English.

Unpacking the --user Flag

Before diving in, let’s clarify what the pip install --user command actually does. According to pip install --help, it instructs pip to:

--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.)

In simpler terms, using pip install --user changes the default location for Python package installations. Instead of installing packages in system-wide directories, which often require admin privileges, this flag installs packages in a user-specific directory, safely isolated from the system-wide locations.

But why does this matter?

Installation Locations: System-Wide vs. User-Specific

By default, pip installs packages in system-wide directories like /usr/local/lib/python${PY_MAJOR}/dist-packages on Unix-like systems. While this approach makes packages accessible to all users on a machine, it comes with a major drawback — the need for administrative access.

Here’s where the --user flag changes things. When you run pip install --user cowsay, pip installs the package in a user-specific location, typically ~/.local/lib/python${PY_MAJOR}.${PY_MINOR}/site-packages. This approach lets you install packages without admin rights and ensures that the packages stay isolated within your user account, reducing conflicts and giving you full control over your Python environment. These user-specific packages remain invisible to other users who access the same host.

The influence of the --user flag extends beyond pip install. It also affects other pip commands like list, aligning their behavior with the user-specific installation location. For instance, pip list --user will display only the packages installed with pip install --user.

Additionally, there’s a convenient trick for those who often prefer user-specific installations: setting the PIP_USER environment variable. By configuring PIP_USER=1 in your environment, you tell pip to default to --user for all installations, eliminating the need to specify the flag every time.

No Virtual Environments? No Problem!

Using pip install --user narrows the scope of pip to operate within the local Python package installation for your user account, rather than the system-wide location. This distinction is crucial in multi-user setups. Anything installed in the system location becomes visible to all users, potentially causing version conflicts if a package’s dependencies differ across users. So, opting for the --user flag is a wise choice, especially when you need packages without root/sudo access or admin privileges.

If you’re the only user on your machine, the difference between user and system locations is minimal. The former simply places packages in a different spot, which may or may not need to join your system’s PATH, depending on the package’s purpose. Some packages come with command-line tools that need to be added to the PATH for easy terminal access.

To find your installation directory, you can use some Python magic:

import site
print(site.USER_SITE)

You can also customize this path by setting the PYTHONUSERBASE environment variable, updating the value of site.USER_BASE. For example, if you want to install some_pkg in an environment with a customized site.USER_BASE of /myappenv, do this:

export PYTHONUSERBASE=/myappenv
python -m pip install --user cowsay

The Role of Virtual Environments

The --user flag, however, isn’t compatible with pip inside a virtual environment (by default created with the --no-site-packages flag). It simply doesn’t make sense in that context. If you try to use --user within an active venv/virtualenv, you’ll see an error like this:

ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

If you create a virtual environment with the --system-site-packages option, the package installed with the --user flag will be placed in the user-specific directory as expected. However, because the virtual environment inherits access to system site-packages, it may also detect the package installed with --user in the system site-packages, potentially leading to conflicts if versions differ between the system and user-specific locations.

Conclusion

In summary, the pip install --user command is a valuable tool for managing Python packages in your personal environment. It allows you to install packages without administrative hassles, keeps installations isolated, and is especially useful in multi-user settings.

For maximum flexibility and to avoid potential version conflicts, consider using virtual environments. They bypass the complexity of --user and ensure a seamless Python experience.

Additional Resources

Thank you for reading!

Any questions? Leave your comment below to start fantastic discussions!

Check out my blog or come to say hi 👋 on Twitter or subscribe to my telegram channel. Looking forward to hearing from you!

--

--

Kirill Bobrov
Kirill Bobrov

Written by Kirill Bobrov

helping robots conquer the earth and trying not to increase entropy using Python, Data Engineering, ML. Check out my blog—luminousmen.com

No responses yet