pip安装失败,出现以下错误:OSError:[Errno 13]目录权限被拒绝

问题:pip安装失败,出现以下错误:OSError:[Errno 13]目录权限被拒绝

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'

pip install -r requirements.txt fails 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)

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'

回答 0

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

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

选项b)安装在您的homedir中:

pip install --user -r requirements.txt

我的建议使用安全(a)选项,以便该项目的需求不会干扰其他项目的需求。

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.


回答 1

我们真的应该停止建议sudowith 的使用pip install。最好先尝试一下pip install --user。如果失败了,请查看此处的最高职位。

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

当您使用进行pip操作时sudo,您将以root用户身份从Internet运行任意Python代码,这会带来很大的安全风险。如果有人在PyPI上放置了一个恶意项目,然后安装了该项目,则可以使攻击者具有对计算机的根访问权限。

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.


回答 2

您试图在系统范围内的路径上安装软件包,而无须这样做。

  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

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


回答 3

只是澄清在Linux(基于ubuntu)上由于权限被拒绝的错误而遭受了很多痛苦之后,什么对我有用,并利用了上面Bert的回答,我现在使用…

$ pip install --user <package-name>

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

$ pip install --user -r requirements.txt

并且这些功能对于每个pip安装(包括创建虚拟环境)都可靠地起作用。

然而,干净的解决方案在我进一步的经验已经安装python-virtualenv,并virtualenvwrappersudo apt-get install在系统级。

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

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.


回答 4

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

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

因此,您应该授予权限,然后尝试再次安装它,如果您有新路径,还应该授予权限:

sudo chown -R $USER /usr/local/lib/python2.7/

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/

回答 5

如果需要权限,则不能将’pip’与’sudo’一起使用。您可以做一个技巧,以便可以使用“ sudo”并安装软件包。只需在您的pip命令前面放置“ sudo python -m …”即可。

sudo python -m pip install --user -r package_name

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

回答 6

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

rm ~/.pydistutils.cfg

然后一切正常。当然,如果确实有原因在该文件中有一些配置,那么您将不希望直接对该文件进行管理。但是,如果其他任何人都做了该解决方法,却忘了删除该文件,这对我来说就成功了!

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 workaround listed 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…

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!


回答 7

是适当的许可问题,

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

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

或尝试

pip install --user -r package_name

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

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

It is due permission problem,

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

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

or try,

pip install --user -r package_name

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

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