Defaulting to user installation because normal site-packages is not writeable
Tags: python, abotwrotethis
Published: 2023-04-11 01:28:02

As a Python programmer, you rely on the standard library packages and third-party packages in most of your projects. These packages come with your Python installation, often stored in a directory called site-packages. However, sometimes you might encounter an error message that reads "normal site-packages directory not writeable." In this case, you might want to consider defaulting to a user installation as a workaround. In this blog post, we'll explore what causes this error, why defaulting to a user installation is a viable solution, and how you can implement it in your Python projects.

## What Causes the "Normal Site-Packages Directory Not Writeable" Error?

First, let's understand what the error message indicates. When you install a package in Python, it usually gets placed in the site-packages directory. However, if your system throws up an error message that reads "normal site-packages directory not writeable," it means that you don't have permission to write to the default site-packages directory. 

This might happen due to a few reasons:

* You might not have administrative privileges on the system you're working on, and therefore can't modify system-wide files or directories. 
* The user running the Python installation might not have permission to write to the site-packages directory.
* Your site-packages directory is corrupted, and the permissions have been lost during the installation process.

Whatever the reason behind this error, it can be frustrating because it prevents you from installing or updating third-party packages in your Python environment.

## Why Default to User Installation as a Solution?

If you encounter the "normal site-packages directory not writeable" error, the easiest solution is to default to a user installation. User installation refers to installing packages in a local directory for the current user instead of a system-wide directory. By default, Python installation provides a local directory called `~/.local`. 

This solution not only solves your permission issues but also offers a few advantages over installing packages in the system-wide site-packages directory:

1. **No administrative privileges required** - when you default to a user installation, you don't need administrative privileges to install third-party packages. 
2. **Isolation from system installations** - The user installation creates a separate repository of packages that are not mixed with system installations. This means that you can install different versions of the same package without conflict.
3. **Better per-project environment management** - With user installation, you can create different environment configurations in different projects, which is not possible with the site-packages directory.

## How to Default to User Installation in Python?

So now that we've discussed why you should default to a user installation let's see how you can do it in Python. There are two ways to switch to a user installation:

### 1. Use the `--user` flag with pip

Pip is the most popular tool for installing third-party packages. To default to a user installation, you can use the `--user` flag when installing packages with pip.

```python
pip install package-name --user
```

This command installs the package in the current user's local directory `.local`.

### 2. Configure default installation directory

Alternatively, you can configure Python to use a new default installation directory for packages. You can do this by modifying the `PYTHONUSERBASE` variable in your environment.

```python
export PYTHONUSERBASE=/path/to/user/dir
```

By setting this variable, you can change the default directory where pip installs packages. Any packages you install with pip using this configuration will be installed in the specified directory.

## What Are the Disadvantages of Defaulting to User Installation?

While defaulting to user installation is a useful workaround for the "normal site-packages directory not writeable" error, it is not a silver bullet. Here are some disadvantages you should keep in mind when using user installation:

1. **Increased storage consumption** - User installations may take up more disk space because each Python environment can have its separate repository of packages.
2. **Management overhead** - Managing multiple user installations can be overwhelming, and you may need to create separate virtual environments for different projects.
3. **Package availability** - Some packages may not support user installation or may have compatibility issues with the current Python version.

## Conclusion

In conclusion, if you encounter the "normal site-packages directory not writeable" error, you can avoid it by defaulting to a user installation. This solution is easy to implement and offers various advantages over system-wide installations. However, keep in mind that there are some drawbacks to user installations, such as increased storage consumption and management overhead. In summary, user installation is an excellent workaround that you can resort to in case you encounter the "normal site-packages directory not writeable" error.