Pip Is Not Able to Install Packages Correctly: Permission Denied Error

Could not install packages due to an EnvironmentError: [Errno 13]

If you want to use python3+ to install the packages you need to use pip3 install package_name

And to solve the errno 13 you have to add --user at the end

pip3 install package_name --user

EDIT:

For any project in python it's highly recommended to work on a Virtual enviroment, is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.

In order to create one with python3+ you have to use the following command:

virtualenv enviroment_name -p python3

And then you work on it just by activating it:

source enviroment_name/bin/activate

Once the virtual environment is activated, the name of your virtual environment will appear on left side of terminal. This will let you know that the virtual environment is currently active.
Now you can install dependencies related to the project in this virtual environment by just using pip.

pip install package_name

Permission denied with pip install --user -e /home/me/package/

It's a bug. The issue is being tracked at https://github.com/pypa/pip/issues/7953 .

The workaround for now is to modify your setup.py() to contain this:

import site
import sys
site.ENABLE_USER_SITE = "--user" in sys.argv[1:]

Or to use something like this on the command line, if you use setup.cfg or pyproject.toml:

$ python3 -c 'import setuptools, site, sys; site.ENABLE_USER_SITE = 1; sys.argv[1:] = ["develop", "--user"]; setuptools.setup()'

ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/var/project_env/bin/pip'

Don't create your virtual environment (venv) in /var.

My advice:

  • Create your virtual environment alongside your source code, if you're using pip
  • If you're using pipenv, let pipenv choose where to create the venv

Each one of your colleagues should have their own venv. You can syncronize your environments by using a requirements.txt (or a Pipfile) to specify which packages and versions are needed.

Why is permission denied on pip install except for when --user is included at end of command?

  • With a virtual environment activated, pip install spam tries to install into the virtual environment's site-packages. This will almost always be somewhere you have write permissions for.

  • pip install --user spam tries to install into the user-packages directory. This will always be somewhere under your home directory, so you should always have write permissions for it.

  • pip install spam tries to install files into the site-packages directory for your Python installation. This will usually not be in your home directory (typically it's somewhere in /Library), so you may or may not have write permissions.

    • Apple's pre-installed Python does not give you write permissions to its site-packages.

      • sudo pip install spam will let you ignore the permissions by installing as root, although with some Python installations it may cause other problems.
    • Homebrew, Python.org, and Anaconda/Miniconda do give you write permissions to their site-packages if you leave the defaults alone.

      • Obviously, leave the defaults alone if you know what you're doing.
    • Less common ways of installing (Enthought, building from source, MacPorts, etc.), you should read the appropriate docs.

So, most likely, you're using a third-party Python and/or an active virtual environment on the machines where pip install spam works, but you're using Apple's pre-installed Python on the ones where it doesn't.

While you could fix that by using sudo, you probably don't want to, for a few reasons:

  • On recent versions of macOS, Apple's pre-installed Python, and the packages they pre-install with it, are badly out of date.
  • The pre-installed packages are set up to be maintained with the deprecated easy_install rather than pip, so getting them up to date can be a huge pain.
  • If you mess things up too badly, you can break some system scripts that the OS depends on.
  • Your changes can be undone by a macOS system update.

So, a better solution is to install Homebrew/Anaconda/Python.org Python if you can, and also use virtual environments when you can and --user whenever possible when you can't. Any one of these three will solve your problem, but you really should do all of them.

And then, if you accidentally try to install something to Apple's site-packages, you'll get a permissions error—but that's a good thing; it means you didn't actually change anything, so you have nothing to undo.

pip install failing with: OSError: [Errno 13] Permission denied on directory

Option a) Create a virtualenv, activate it and install:

virtualenv .venv
source .venv/bin/activate
pip install -r requirements.txt

Option b) Install in your homedir:

pip install --user -r requirements.txt

My recommendation use safe (a) option, so that requirements of this project do not interfere with other projects requirements.

Module not found error in Python after pip install package --user

You're probably attempting to download package to a system folder where you don't have permission to write or modified, maybe it's due to modification on your pip.ini file which specify by default the packages location ?

Check pip documentation and his topic about configuration here.

Furhtermore, I advise you, if you start learning python and using pip package, to learn fundamental about the principle of virtualenv using venv lib here. VirtualEnv is a recommended practice in python who allow you to compartmentalize project in a specified place with a personal pip package directory and other things, these methods aim to avoid dependancy concurrence between projects and also avoid pip package permissions issue by downloading pip package only for your virutalenv.

  • Step by step to setup venv ( you should be in the root folder of your
    projet) :
user@hostname > python3 -m venv venv
user@hostname > source ./env/bin/activate
(venv) user@hostname > python -m pip install foo-packages
  • Or the second option but not recommended is to use sudo to
    install packages to the system folder :
sudo@hostname > sudo python -m pip install foo-packages


Related Topics



Leave a reply



Submit