- [Solved] Defaulting to user installation because normal site-packages is not writeable
- Defaulting to user installation because normal site-packages is not writeable
- Issue 1: Multiple Versions of Python
- Solution 1 – Specify Python Interpreter while installing packages
- Solution 2 – Install Virtual Environment
- Issue 2 – Permission Issue
- Solution – Install Python for specific user and grant permissions
- Conclusion
- site — Site-specific configuration hook¶
- Defaulting to user installation because normal site-packages is not writeable
- Why this message occurs
- How to solve for Windows users
- How to solve for Linux users
- How to solve for Mac users
- Conclusion
- Take your skills to the next level ⚡️
- About
- Search
- Tags
[Solved] Defaulting to user installation because normal site-packages is not writeable
If you have multiple versions of Python and try to install the packages using the pip3 install command Python will throw an error defaulting to user installation because normal site-packages is not writeable.
In this article, we will take a look at what is defaulting to user installation because normal site-packages is not writeable and how to fix this error.
Defaulting to user installation because normal site-packages is not writeable
There are several reasons behind this error and some of the most common issues and solutions are mentioned below.
Issue 1: Multiple Versions of Python
In the case of Linux environments, Python comes by default and the version of Python depends on a different distribution of Linux and you may have installed a different version of Python.
Now when you are using pip install command to install the packages it will throw the error site-packages are not writable.
The reason behind this is that you have multiple versions of Python and since you are using pip / pip3 it would try to add the packages in the default version of Python which is managed by Python and hence it will throw an error.
Solution 1 – Specify Python Interpreter while installing packages
To install the packages try running the below command by appending the Python command to it. This will ensure the correct Python interpreter is specified and the packages will be installed over there.
python3 -m pip install [package_name]
python -m pip install [package_name]
If this also throws an error the better way is to resolve this issue is to append the exact version of Python will installing the packages.
Install Package using Specific Python Interpreter version
python3.7 -m pip install [package_name]
Solution 2 – Install Virtual Environment
A virtual environment creates an isolated Python virtual environment and keeps all the dependencies for the project inside it. This would be the ideal solution to resolve the conflict.
Follow the steps to create the virtual environment for your project.
Step 1: Install Virtual Environment Module in Python
Step 2: Verify if the Virtual environment is installed properly by checking its version
Step 3: Create a new virtual environment for your project
After running this command, a directory named my_name will be created. This is the directory that contains all the necessary executables to use the packages that a Python project would need. This is where Python packages will be installed.
Step 4: Specify the Python interpreter of your choice. This will be useful if you have multiple versions of Python installed.
$ virtualenv -p /usr/bin/python3 virtualenv_name
Step 5: Activate the Virtual Environment
$ source virtualenv_name/bin/activate
Issue 2 – Permission Issue
If you have multiple user accounts in Linux/Mac/Windows do check if the Python is installed for specific users or all the users. Many times if it’s accessible only by specific users and hence you get an error that site-packages are not writeable.
Solution – Install Python for specific user and grant permissions
Verify if Python is installed for all the users and has the correct permissions to write and install the packages.
Conclusion
Mostly when you have multiple version of Python installed on your machine and if your OS also comes with default version of Python there would be a conflict and when you install the packages Python will throw defaulting to user installation because normal site-packages is not writeable.
The best way to resolve this issue is by Creating the virtual environment or specifying the exact version of Python interpreter while installing the packages.
site — Site-specific configuration hook¶
This module is automatically imported during initialization. The automatic import can be suppressed using the interpreter’s -S option.
Importing this module will append site-specific paths to the module search path and add a few builtins, unless -S was used. In that case, this module can be safely imported with no automatic modifications to the module search path or additions to the builtins. To explicitly trigger the usual site-specific additions, call the site.main() function.
Changed in version 3.3: Importing the module used to trigger paths manipulation even when using -S .
It starts by constructing up to four directories from a head and a tail part. For the head part, it uses sys.prefix and sys.exec_prefix ; empty heads are skipped. For the tail part, it uses the empty string and then lib/site-packages (on Windows) or lib/python X.Y /site-packages (on Unix and macOS). For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to sys.path and also inspects the newly added path for configuration files.
Changed in version 3.5: Support for the “site-python” directory has been removed.
If a file named “pyvenv.cfg” exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory and it is also checked for site-packages (sys.base_prefix and sys.base_exec_prefix will always be the “real” prefixes of the Python installation). If “pyvenv.cfg” (a bootstrap configuration file) contains the key “include-system-site-packages” set to anything other than “true” (case-insensitive), the system-level prefixes will not be searched for site-packages; otherwise they will.
A path configuration file is a file whose name has the form name .pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path . Non-existing items are never added to sys.path , and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.
An executable line in a .pth file is run at every Python startup, regardless of whether a particular module is actually going to be used. Its impact should thus be kept to a minimum. The primary intended purpose of executable lines is to make the corresponding module(s) importable (load 3rd-party import hooks, adjust PATH etc). Any other initialization is supposed to be done upon a module’s actual import, if and when it happens. Limiting a code chunk to a single line is a deliberate measure to discourage putting anything more complex here.
For example, suppose sys.prefix and sys.exec_prefix are set to /usr/local . The Python X.Y library is then installed in /usr/local/lib/python X.Y . Suppose this has a subdirectory /usr/local/lib/python X.Y /site-packages with three subsubdirectories, foo , bar and spam , and two path configuration files, foo.pth and bar.pth . Assume foo.pth contains the following:
# foo package configuration foo bar bletch
# bar package configuration bar
Defaulting to user installation because normal site-packages is not writeable
When installing Python packages using pip , you might encounter the following message in the console:
This message appears when pip fails to install the package in the system-wide directory.
This is not an error message as pip can still install the package, but you might have problems like unable to access the package from your terminal as follows:
This tutorial explains why this message occurs and how you can make it disappear in practice.
Why this message occurs
By default, pip tries to install your package in the system-wide directory named site-packages .
When you lack permission to write to system-wide site-packages , then pip will install it in the user-wide site-packages instead.
You can verify this by running python -m site command from the terminal. There should be multiple site-packages directory as shown below. The screenshot is from a Macbook:
The one under /Applications/Xcode.app is the system-wide directory that requires root permission to write.
The USER_SITE one is where pip installs packages when the “Defaulting to user installation” message appears.
How to solve for Windows users
If you’re using Windows, then this message likely occurs because you installed Python under C:\Program Files which requires administrator permission to modify.
To remove the message, you need to add the —user flag when installing packages:
If you want to permanently fix this, you need to uninstall Python from your computer and install it again using the default settings.
If you need to customize Python installation, then take note of the default installation path and use it when you customize the install location:
By using the location provided in Python default installation, you will have Python installed in a folder that’s writeable without admin permission.
Also, don’t forget to check the options ‘Use admin privileges’ and ‘Add python.exe to PATH’.
Try running pip install command again and you won’t receive the message this time.
How to solve for Linux users
If you’re using Linux, then you will get this message because you installed python using sudo as in:
In Linux-based OS, the system-wide packages directory is also known as dist-packages , and you need root permission to access this directory.
Again, you can verify this using the python -m site command:
When pip fails to install to the dist-packages directory, the message appears to notify you that it’s installing to the site-packages directory instead.
To remove the message, you need to add the —user flag when installing packages:
This command tells pip to skip the dist-packages directory and go to site-packages directly.
If you want to install to dist-packages , then you need to add sudo to the install command:
But you might get a warning message in the console as follows:
If you want to permanently fix this message, then you need to install Python without using sudo . This means you can’t use apt-get either.
You can get Python in Linux using Homebrew, or you can download the source and compile it yourself.
Follow the steps below to compile Python manually. Note that you can get the full path to Python .tgz file from here:
The make command might take a while to complete. Once done, you should now have python3 and pip3 available from your user directory.
Use the which command to verify the installation:
Alright. Now you can install packages without using sudo command:
Because you installed Python and pip without using sudo , the default installation will use the site-packages directory.
Keep in mind that installing Python from source like this enables Python only for the current user. You need to repeat the process for other users.
How to solve for Mac users
If you’re using a Mac, this message appears when you use Python and pip that are provided by the system.
It’s recommended to install your own Python version to avoid permission problems and future changes from the macOS itself.
You can install Python using Homebrew as follows:
Once installed, you should open a new Terminal window for the changes to take affect.
You can see the active python3 and pip3 versions using the which command as follows:
Once both python3 and pip3 are using the Homebrew-sourced version, you’re good to go.
Install any packages using pip install and you won’t receive the error message.
Conclusion
Now you’ve learned how to solve the “Defaulting to user installation” message that occurs when installing Python packages.
I hope this tutorial is helpful. Until next time! 👋
Take your skills to the next level ⚡️
I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Tags
Click to see all tutorials tagged with: