Python pip 安装失败:OSError: [Errno 13] 目录权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31512422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:07:54  来源:igfitidea点击:

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

pythonpermissionspipinstall

提问by RunLoop

pip install -r requirements.txtfails with the exception below OSError: [Errno 13] Permission denied: '/usr/local/lib/.... What's wrong and how do I fix this? (I am trying to setup Django)

pip install -r requirements.txt失败,但出现以下异常OSError: [Errno 13] Permission denied: '/usr/local/lib/...。出了什么问题,我该如何解决?(我正在尝试设置Django

Installing collected packages: amqp, anyjson, arrow, beautifulsoup4, billiard, boto, braintree, celery, cffi, cryptography, Django, django-bower, django-braces, django-celery, django-crispy-forms, django-debug-toolbar, django-disqus, django-embed-video, django-filter, django-merchant, django-pagination, django-payments, django-storages, django-vote, django-wysiwyg-redactor, easy-thumbnails, enum34, gnureadline, idna, ipaddress, ipython, kombu, mock, names, ndg-httpsclient, Pillow, pyasn1, pycparser, pycrypto, PyJWT, pyOpenSSL, python-dateutil, pytz, requests, six, sqlparse, stripe, suds-jurko
Cleaning up...
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1436, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 672, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/usr/lib/python2.7/dist-packages/pip/req.py", line 902, in move_wheel_files
    pycompile=self.pycompile,
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 206, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/lib/python2.7/dist-packages/pip/wheel.py", line 193, in clobber
    os.makedirs(destsubdir)
  File "/usr/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/amqp-1.4.6.dist-info'

采纳答案by hectorcanto

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

选项 a) 创建一个 virtualenv,激活它并安装:

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

Option b) Install in your homedir:

选项 b) 在您的 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.

我的建议是使用 safe (a) 选项,这样这个项目的需求就不会干扰其他项目的需求。

回答by Tobia Tesan

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 sudoto temporarily obtain superuserpermissions at your responsibilityin order to install the package on the system-wide path:

    sudo pip install -r requirements.txt
    

    Find more about sudohere.

    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-userpath using the --userflag.

    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 projectsand 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 noton the system-wide path):

    pip install -r requirements.txt

  1. 通常,您可以使用sudo临时获取由您负责的超级用户权限以便在系统范围的路径上安装软件包:

    sudo pip install -r requirements.txt
    

    sudo在此处了解更多信息。

    实际上,这是一个坏主意,没有好的用例,请参阅@wim 的评论。

  2. 如果您不想进行系统范围的更改,您可以使用该标志将软件包安装在您的每用户路径上--user

    只需要:

    pip install --user runloop requirements.txt
    
  3. 最后,对于更细粒度的控制,您还可以使用virtualenv,这可能是开发环境的最佳解决方案,特别是如果您正在处理多个项目并希望跟踪每个项目的依赖项。

    激活您的 virtualenv 后

    $ my-virtualenv/bin/activate

    以下命令将在 virtualenv 中安装包(而不是在系统范围的路径上):

    pip install -r requirements.txt

回答by bert

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

我们真的应该停止建议使用sudowith pip install。最好先试一下pip install --user。如果此操作失败,请查看此处的顶级帖子。

The reason you shouldn't use sudois as follows:

不应该使用的原因sudo如下:

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.

当你使用 pip 运行时sudo,你是以 root 用户身份运行来自互联网的任意 Python 代码,这是一个相当大的安全风险。如果有人在 PyPI 上放置了一个恶意项目并且你安装了它,你就给了攻击者对你的机器的 root 访问权限。

回答by Thom Ives

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 ...

在 linux(基于 ubuntu)中对权限被拒绝的错误进行了很多痛苦之后,只是澄清对我有用的东西,并利用上面 Bert 的回答,我现在使用......

$ pip install --user <package-name>

or if running pip on a requirements file ...

或者如果在需求文件上运行 pip ...

$ pip install --user -r requirements.txt

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

这些对于每个 pip 安装都可靠地工作,包括创建虚拟环境。

However, the cleanest solution in my further experiencehas been to install python-virtualenvand virtualenvwrapperwith sudo apt-get installat the system level.

但是,根据我的进一步经验,最干净的解决方案在系统级别安装python-virtualenvvirtualenvwrapper使用sudo apt-get install

Then, inside virtual environments, use pip installwithout the --userflag ANDwithout sudo. Much cleaner, safer, and easier overall.

然后,在虚拟环境中,使用pip install不带--user标志不带sudo. 整体上更清洁、更安全、更容易。

回答by Mesut GUNES

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

用户对某些 Python 安装路径没有写权限。您可以通过以下方式授予权限:

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/

回答by bwest87

So, I got this same exact error for a completely different reason. Due to a totally separate, but known Homebrew + pip bug, I had followed this workaroundlisted on Google Cloud's help docs, where you create a .pydistutils.cfg file in your home directory. This file has special config that you're only supposed to use for your install of certain libraries. I should have removed that disutils.cfg file after installing the packages, but I forgot to do so. So the fix for me was actually just...

所以,由于完全不同的原因,我得到了同样的确切错误。由于完全独立但已知的Homebrew + pip 错误,我遵循了 Google Cloud 帮助文档中列出的此解决方法,您可以在其中在主目录中创建一个 .pydistutils.cfg 文件。此文件具有特殊的配置,您仅应将其用于安装某些库。我应该在安装软件包后删除该 disutils.cfg 文件,但我忘记这样做了。所以对我来说修复实际上只是......

rm ~/.pydistutils.cfg.

rm ~/.pydistutils.cfg.

And then everything worked as normal. Of course, if you have some config in that file for a real reason, then you won't want to just straight rm that file. But in case anyone else did that workaround, and forgot to remove that file, this did the trick for me!

然后一切正常。当然,如果您出于真正的原因在该文件中有一些配置,那么您不会只想直接 rm 该文件。但是,万一其他人做了这种解决方法,而忘记删除该文件,这对我有用!

回答by Mohideen bin Mohammed

It is due permission problem,

这是由于权限问题,

sudo chown -R $USER /path to your python installed directory

default it would be /usr/local/lib/python2.7/

默认是 /usr/local/lib/python2.7/

or try,

或尝试,

pip install --user -r package_name

and then say, pip install -r requirements.txtthis will install inside your env

然后说,pip install -r requirements.txt这将安装在您的环境中

dont say, sudo pip install -r requirements.txtthis is will install into arbitrary python path.

不要说,sudo pip install -r requirements.txt这将安装到任意python路径中。