5

I am trying to run a small python code (which requries pytz and some other packages) on a aws ec2 instance. When I tried to install pytz, I got some errors:

[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'

I followed the accepted answer of this topic but got another error:

sudo:pip: command not found

Then I found out it might be because the path setting is wrong (according to this page. But I could not manage to Just add ~/.local/bin to your path as suggested by the second answer.

Can someone show me how to add the bin to my path? I have no idea.

And some more info:

[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]$
Community
  • 1
  • 1
user3768495
  • 4,077
  • 7
  • 32
  • 58

3 Answers3

14

The default (secure_path) for sudo is specified in /etc/sudoers.

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

Since pip is not found in that path, you are getting the not found error. Preserve your PATH when running sudo by:

sudo env "PATH=$PATH" pip install pytz
helloV
  • 50,176
  • 7
  • 137
  • 145
  • 1
    I did `sudo "PATH=$PATH" pip install pytz` but it's still getting same error. It's like this: [ec2-user@ip-172-31-28-178 ug]$ sudo grep secure_path /etc/sudoers Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin [ec2-user@ip-172-31-28-178 ug]$ sudo "PATH=$PATH" pip install pytz sudo: pip: command not found – user3768495 Nov 19 '16 at 20:10
  • did i miss anything here? Thank you @helloV! – user3768495 Nov 19 '16 at 20:11
  • What is the output of `echo $PATH` and `which pip` – helloV Nov 19 '16 at 20:12
  • [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]$ – user3768495 Nov 19 '16 at 20:12
  • Please see the question edits for better presentation of the outputs. – user3768495 Nov 19 '16 at 20:15
  • Great! the `sudo env "PATH=$PATH" pip install pytz` worked! Please update your answer and I will accept it as the final answer, if you'd like. I really appreciate your help @helloV. – user3768495 Nov 19 '16 at 20:23
  • 2
    how do you avoid writing env"path=$path" and add it permanently? – ARKhan Sep 02 '17 at 10:01
  • Try `pip-3.5` or `pip-2.7`. This is what worked for me on Amazon EC2-Instance e.g., `pip-3.5 install ldap3`. – Kyle Bridenstine Jun 01 '18 at 17:43
  • 1
    In order to avoid writing env "path=$path" every time you can create an alias: `alias pip='/usr/bin/pip` . Also for other pip versions: `alias pip3='/usr/bin/pip-3.6'`. It worked for me. – Diego Nov 20 '18 at 00:53
1

In general using an absolute path or alias for pip(as recommended by user Diego in comments above) seems to be a good way to know what you are really doing, on EC2 instance I used therefore:

sudo /opt/python/run/venv/bin/pip3 install pycurl
Greg Holst
  • 874
  • 10
  • 23
0

If you might want to try: pip3 install <your - package name>

Joe225
  • 1
  • 1