本文介绍了为什么没有Python和pip的sudo不能做一些事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用pip时,没有sudo通常无法正常工作.我经常看到人们在不使用sudo的情况下使用pip,所以我在做什么错了?

When I use pip it usually doesn't work without sudo. I often see people use pip without sudo, so what am I doing wrong?

我了解到不建议使用sudo安装pip软件包.我知道使用virtualenv我可以在不使用sudo的情况下使用pip,但是要安装virtualenv我必须先使用sudo.

I read that it is not recommended to install pip packages with sudo. I know that with virtualenv I can use pip without sudo, but to install virtualenv I have to use sudo first.

当我尝试不使用sudo安装pip时,会得到:

When I try to install pip without sudo, I get:

PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/pip'

在尝试使用pip3 install flask安装flask时:

When trying to install flask with pip3 install flask:

PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.4/dist-packages/werkzeug'

推荐答案

sudo 在Unix/Linux系统中用于使用另一个用户的权限,使用他们的权限,例如写入某些目录的能力.当您不指定要模拟的用户时,例如在运行时

sudo is used in Unix/Linux systems to perform tasks as another user, using their permissions, such as the ability to write to certain directories. When you don't specify the user to emulate, such as when running

sudo pip install flask

您正试图以系统管理员的身份运行命令,在许多环境中称为root.系统将要求您输入管理员密码(如果您已授予用户admin特权,则可以是您自己的管理员密码),然后指定的命令以该用户的身份运行,这意味着该命令具有读/写权限基本上可以访问系统上的每个文件和目录(有一些例外,但是它们大多是极端情况,在这里不是很重要).这意味着您在使用sudo时需要特别小心 ,因为只有单个空格的错误确实会把事情弄得一团糟:比较

you are attempting to run the command as the system administrator, known as root in many environments. You'll be asked for the administrator password (which can be your own, if you've given your user admin privileges), then the specified command runs as that user, meaning that it has read/write access to essentially every file and directory on the system (there are some exceptions, but they're mostly corner cases and not very important here). This means that you need to be extra careful when using sudo, as an error as small as a single space can really mess things up: compare

sudo rm -rf /usr/local/lib/python3.4/dist-packages/numpy*

使用

sudo rm -rf /usr /local/lib/python3.4/dist-packages/numpy*

看到/usrlocal/之间有空格吗?您刚刚开始删除整个/usr文件夹,其中包含系统上重要文件和程序的很大一部分.希望你有备份!现在,这并不意味着您需要惊吓sudo死亡,但是您 do 需要对此有一个健康的尊重.

See that space between /usr and local/? You just started deleting the entire /usr folder, which contains a large portion of the vital files and programs on the system. Hope you have a backup! Now, this doesn't mean you need to be scared to death of sudo, but you do need to have a healthy respect for it.

Python安装通常是系统级的(是的,我知道有例外),这意味着您需要使用sudo进行修改,例如在使用pip安装第三方模块时.如果您运行

Python installations tend to be system-level (yes, I know there are exceptions), which means you need to use sudo to modify them, such as when installing 3rd-party modules with pip. If you run

ls -l /usr/local/lib/python3.4

您会看到类似

drwxrwsr-x 125 root 4096 Nov  3 00:40 dist-packages

表明您要使用pip安装到的目录归root所有,因此必须使用sudo.

showing that the directory you're trying to install to with pip is owned by root, so the use of sudo is necessary.

现在,有两种方法可以解决此问题.如果您对此感到满意,并且不介意修改系统的全局软件包,请继续将sudopip一起使用(实际上,如果您看到黄色的小消息,则可能需要使用sudo -H ...一开始是关于主目录中的权限的信息).您所有的模块都将安装到/usr/local/lib/python3.4/dist-packages,并且系统上的所有用户均可使用.

Now, there are a couple of ways around this. If you're comfortable with it, and don't mind modifying the system's global packages, go ahead and use sudo with pip (in fact, you may need to use sudo -H ... if you get a little message in yellow at the beginning about permissions in your home directory). All of your modules will be installed to /usr/local/lib/python3.4/dist-packages and be available to all users on the system.

第二个选项是使用pip的 --user 选项,这将在主目录(~)中创建一个lib/python3.4/site-packages层次结构,并将所有已安装的模块以及脚本存储在~/bin中(应将其添加到$PATH中.此方法的优点是不需要,您无需使用sudo,因此您不会意外覆盖需要其他版本才能运行其他程序的系统相关模块,缺点是安装的模块仅可用对您来说,例如,如果您的Web服务器试图以自己的身份运行Flask并且无法读取源文件,您可能会遇到问题,但是,这只是一个小小的配置文件编辑无法解决.这是我为大多数用户推荐的解决方案.

The second option is to use pip's --user option, which will create a lib/python3.4/site-packages hierarchy in your home directory (~) and store any installed modules there, along with scripts in ~/bin (which you should add to your $PATH. The advantage of this method is that you don't need to use sudo, so you won't accidentally overwrite system-dependent modules where specific versions are required for other programs to run. The disadvantage is that the installed modules are only available to you, so you may run into issues if, for example, your web server is trying to run Flask as itself, and can't read the source files. However, that's nothing that a little config file-editing can't fix. This is my recommended solution for most users.

第三个选择是使用虚拟环境,例如 virtualenv .这将在您选择的位置创建一个自定义Python安装,并带有单独 python可执行文件和site-packages层次结构(您可以选择是否链接到或使用系统的dist-packages存储库).您可以pip install直接将其打包到virtualenv中,并根据自己的意愿创建尽可能多的环境,例如,每个环境的各种依赖项版本略有不同,因此可以更健壮地测试程序.您可以打开和关闭虚拟环境,例如,可以让几个在终端的不同选项卡中运行,例如,并行测试.这是我的第二建议,因为在激活和使用环境中(涉及)更多的工作,如果您对命名环境不太满意,您可能会感到困惑.缺点包括缺乏系统范围的可用性(如第二个选项),以及需要在使用前手动激活虚拟环境的事实.

The third option is to use virtual environments like virtualenv. This will create a custom Python installation in the location of your choosing, with a separate python executable and site-packages hierarchy (there are options as to whether or not you want to link to or use the system's dist-packages repository). You can pip install packages directly into the virtualenv, and make as many environments as your little heart desires, each with slightly different versions of various dependencies, for example, so you can more robustly test your programs. You can turn on and off the virtual environments, so for example you could have a couple running in different tabs of Terminal, for example, testing things in parallel. This is a close second-place recommendation of mine, because there's (slightly) more work involved in activating and using the environments, and you may get confused about which one you're working in if you're not very good about naming them. Downsides include the lack of system-wide availability, like the second option, and the fact that the virtual environment needs to be manually activated before use.

因此,请看一下这些选项,然后看看哪种最适合您的系统和您的特定情况.祝你好运!

So, take a look at the options, and see which is best for your system and your particular situation. Good luck!

这篇关于为什么没有Python和pip的sudo不能做一些事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:05