大网数据广告活动
服务器运维问答

linux command found but not found when using sudo

· 52CE 我爱测

I am trying to run a python3 install like this: pip3 install django I get the error: PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/pytz' I then try: sudo pip3 install django I get the erro…

问题原文

原帖链接 · petey · CC BY-SA 3.0

I am trying to run a python3 install like this:

pip3 install django

I get the error:

PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/pytz'

I then try:

sudo pip3 install django

I get the error:

sudo: pip3: command not found

Here is what my $PATH looks like:

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

How can I get sudo access to pip3?

社区高分解答

原帖链接 · EvilTorbalan · CC BY-SA 3.0

Sudo is not using the same PATH as your current user for security reasons. In /etc/sudoers there is an option secure_path which specifies the path used when running commands with sudo. Mine looks like this:

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

From the sudo manpage:

 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.

The pip3 command is probably not in the path defined by the sudoers file. You can either add the path to the secure_path or provide the full path to the pip3 command like this:

sudo /path/to/pip3 install django

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-01-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。