AWS ec2实例上的“sudo pip”时出错

时间:2016-11-19 19:58:59

标签: python amazon-web-services amazon-ec2 pip sudo

我正在尝试在aws ec2实例上运行一个小的python代码(需要pytz和其他一些软件包)。当我尝试安装pytz时,我遇到了一些错误:

[ec2-user@ip-172-31-28-178 ~]$ pip install pytz
Collecting pytz
  Using cached pytz-2016.7-py2.py3-none-any.whl
Installing collected packages: pytz
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python2.7/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/usr/local/lib/python2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/lib/python2.7/site-packages/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/lib/python2.7/site-packages/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/usr/local/lib/python2.7/site-packages/pip/utils/__init__.py", line 83,  in ensure_dir
    os.makedirs(path)
  File "/usr/lib64/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/site-packages/pytz-2016.7.dist-info'

我按照this topic接受的答案,但又出现了另一个错误:

sudo:pip: command not found

然后我发现它可能是因为路径设置错误 (根据this page。但我无法按照第二个答案的建议设法Just add ~/.local/bin to your path

有人可以告诉我如何将垃圾箱添加到我的路径中吗?我不知道。

还有一些信息:

[ec2-user@ip-172-31-28-178 ug]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin
[ec2-user@ip-172-31-28-178 ug]$ which pip
/usr/local/bin/pip
[ec2-user@ip-172-31-28-178 ug]$

2 个答案:

答案 0 :(得分:11)

sudo的默认值(secure_path)在/etc/sudoers中指定。

[ec2-user@ip-172-31-28-178 ~]$ sudo grep secure_path /etc/sudoers
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

由于在该路径中找不到pip,因此您收到了未找到的错误。通过以下方式运行PATH时保留sudo

sudo env "PATH=$PATH" pip install pytz

答案 1 :(得分:0)

通常,对pip使用绝对路径或别名(如迭戈用户在上面的注释中所建议的那样)似乎是了解您实际操作的好方法,因此在我使用的EC2实例上:

sudo /opt/python/run/venv/bin/pip3 install pycurl