How to resolve "normal site-packages is not writable" in Python

In Python, the error message “Defaulting to user installation because normal site-packages is not writeable” usually appears when we try to install packages without having the necessary permissions to write to the system-wide site-packages directory.

Occurrence

This commonly occurs in virtual environments or when we lack administrative access.

Error-resolving approaches

To resolve this error, we can use the following approaches:

  • Using a virtual environment

First, create a virtual environment using tools like venv or virtualenv, using this command in the terminal:

python -m venv myenv

Now, activate the virtual environment using these commands:

source myenv/bin/activate # for Linux/Mac
myenv\Scripts\activate.bat # for Windows

Finally, install packages within the virtual environment using this command in the terminal:

pip install package-name

This way, we’ll have write access to the virtual environment’s site-packages directory without requiring system-wide permissions.

In the following terminal command, we're using Python 3.8 to create a new virtual environment named myenv. You just need to run the command pip install flask or any package you want to install.

Terminal 1
Terminal
Loading...
  • Specifying a user installation

We can install the package in the user’s local site-packages directory using this command:

pip install package-name --user

Use the --user flag when installing packages to specify a user installation instead of a system-wide installation.

Terminal 1
Terminal
Loading...
  • Using a package manager

If you’re using Linux, you can use system-wide package management like apt or yum to install Python packages. Typically, administrative rights are needed for this.

sudo apt install python-package-name # for the apt package manager
sudo yum install python-package-name # for the yum package manager

These approaches provide us with the necessary write permissions to install packages in a controlled and isolated manner.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved