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

前端 未结 8 1461
独厮守ぢ
独厮守ぢ 2020-11-21 22:43

pip install -r requirements.txt fails with the exception below OSError: [Errno 13] Permission denied: \'/usr/local/lib/.... What\'s wrong and how d

相关标签:
8条回答
  • 2020-11-21 22:46

    You are trying to install a package on the system-wide path without having the permission to do so.

    1. In general, you can use sudo to temporarily obtain superuser permissions at your responsibility in order to install the package on the system-wide path:

       sudo pip install -r requirements.txt
      

      Find more about sudo here.

      Actually, this is a bad idea and there's no good use case for it, see @wim's comment.

    2. If you don't want to make system-wide changes, you can install the package on your per-user path using the --user flag.

      All it takes is:

       pip install --user runloop requirements.txt
      
    3. Finally, for even finer grained control, you can also use a virtualenv, which might be the superior solution for a development environment, especially if you are working on multiple projects and want to keep track of each one's dependencies.

      After activating your virtualenv with

      $ my-virtualenv/bin/activate

      the following command will install the package inside the virtualenv (and not on the system-wide path):

      pip install -r requirements.txt

    0 讨论(0)
  • 2020-11-21 22:49

    Just clarifying what worked for me after much pain in linux (ubuntu based) on permission denied errors, and leveraging from Bert's answer above, I now use ...

    $ pip install --user <package-name>
    

    or if running pip on a requirements file ...

    $ pip install --user -r requirements.txt
    

    and these work reliably for every pip install including creating virtual environments.

    However, the cleanest solution in my further experience has been to install python-virtualenv and virtualenvwrapper with sudo apt-get install at the system level.

    Then, inside virtual environments, use pip install without the --user flag AND without sudo. Much cleaner, safer, and easier overall.

    0 讨论(0)
  • 2020-11-21 22:50

    We should really stop advising the use of sudo with pip install. It's better to first try pip install --user. If this fails then take a look at the top post here.

    The reason you shouldn't use sudo is as follows:

    When you run pip with sudo, you are running arbitrary Python code from the Internet as a root user, which is quite a big security risk. If someone puts up a malicious project on PyPI and you install it, you give an attacker root access to your machine.

    0 讨论(0)
  • 2020-11-21 22:54

    If you need permissions, you cannot use 'pip' with 'sudo'. You can do a trick, so that you can use 'sudo' and install package. Just place 'sudo python -m ...' in front of your pip command.

    sudo python -m pip install --user -r package_name
    
    0 讨论(0)
  • 2020-11-21 23:00

    User doesn't have write permission for some Python installation paths. You can give the permission by:

    sudo chown -R $USER /absolute/path/to/directory
    

    So you should give permission, then try to install it again, if you have new paths you should also give permission:

    sudo chown -R $USER /usr/local/lib/python2.7/
    
    0 讨论(0)
  • 2020-11-21 23:13

    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.

    0 讨论(0)
提交回复
热议问题