使用sudo时找到但未找到linux命令


11

我试图像这样运行python3安装:

pip3 install django

我得到错误:

PermissionError:[Errno 13]权限被拒绝:'/usr/local/lib/python3.6/site-packages/pytz'

然后,我尝试:

sudo pip3 install django

我得到错误:

sudo:pip3:找不到命令

这是我的$ PATH的样子:

/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin

如何获得对pip3的sudo访问权限?

Answers:


9

PATH出于安全原因,Sudo与您当前的用户使用的不是同一用户。其中/etc/sudoers有一个选项secure_path,用于指定使用sudo运行命令时使用的路径。我的看起来像这样:

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

从sudo联机帮助页:

 secure_path   Path used for every command run from sudo.  If you don't trust the people running sudo to have a sane PATH environment
               variable you may want to use this.  Another use is if you want to have the “root path” be separate from the “user path”.
               Users in the group specified by the exempt_group option are not affected by secure_path.  This option is not set by
               default.

pip3命令可能不在sudoers文件定义的路径中。您可以将路径添加到secure_path或提供完整的pip3命令路径,如下所示:

sudo /path/to/pip3 install django

1

sudo可能在使用时未保留$ PATH变量。尝试在命令中使用pip3二进制文件的完整路径,例如:sudo /path/to/pip3 install django。如果不确定完整路径,请执行which pip3


哪个pip3输出:/ usr / local / bin / pip3。$ PATH变量看起来也很好:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-用户/ bin
petey

sudo echo $PATH产生什么不同?您是否尝试了使用pip3完整路径的命令?结果有何不同?
萨法多

sudo输出相同:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2
佩蒂


0

我以这种方式发布了django安装:

pip3 install --user django

这解决了问题。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.