You may run into the error “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied” when installing a Python package with pip. Read on to find out why the installation process fails and how to get it right.
The Error “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied”
Exceptions like ModuleNotFoundError require you to install the target module with pip. However, it may produce this error:
pip install twitter Could not install packages due to an EnvironmentError: [Errno 13] Permission denied
When pip installs a package, it fetches all the required files from the repository (Python Package Index) and puts them in one of the dedicated locations for Python packages in your file system. These paths, also known as site-packages directories, allow Python to find and import packages when you need them. The reason for the error above is that pip doesn’t have permission to write into the default installation path.
Usually, it occurs when you run pip to install a package into a system directory. It is owned by the superuser, such as root in Linux. As a normal user, you don’t have permission to write and delete files from it.
Use User Installation Directory
On operating systems like Linux, Python and many packages are typically part of the distribution. You can use the system’s package manager to manage them, including install, upgrade, and uninstall packages.
All those tasks require root access and work in a separate mechanism to pip. The package manager will put Python packages into system directories, such as /usr/lib/python3.10/site-packages. They will be available to all users in the system.
Normal users and pip aren’t supposed to interfere with these components. Doing so may create conflicts between the system’s package manager and pip. As an alternative, you can install packages into a directory of your own.
Python gives each user a user base, where you can install modules, data, C headers, and scripts. On Linux, it is ~/.local/lib/pythonX.Y ~/Library/Python/X.Y/lib/python on macOS, and %APPDATA%\Python\PythonXY on Windows (X.Y is the version of the Python installation).
You don’t need privileged access to write to this location. Your Python scripts and interpreter can discover packages installed to it because its path is always included in sys.path.
To install to your user directory, add the option –user to the pip command:
pip install --user twitter
You can verify the installation path of the package with the show sub-command:
pip show twitter Name: twitter Version: 1.19.6 Summary: An API and command-line toolset for Twitter (twitter.com) Home-page: https://mike.verdone.ca/twitter/ Author: Mike Verdone Author-email: [email protected] License: MIT License Location: /home/admin/.local/lib/python3.10/site-packages
Use Virtual Environments
Virtual environments help you isolate a working environment to a certain extent. Each virtual environment has its own directory where you can install packages for it. You can use a different version of the same package compared to the system-wide installation.
First, set up a virtual environment (replace learnshareit with the name you want):
Linux and macOS
python -m venv learnshareit
py -m venv learnshareit
Python will create the “learnshareit” directory and copy essential management tools to it. Activate the virtual environment you just created:
You can now install packages with pip into this virtual environment:
pip install twitter
Python gives you the error “Could not install packages due to an EnvironmentError: [Errno 13] Permission denied” when you don’t have permission to write into its system-wide installation path. Install to your user directory or use a virtual environment instead.
Maybe you are interested:
- IndexError: list index out of range in Python
- IndexError: Replacement index 1 out of range for positional args tuple
- ModuleNotFoundError: No module named ‘tensorflow’ in python
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Name of the university: HUST