user3768495 user3768495 - 1 year ago 71
Python Question

error when "sudo pip" on an AWS ec2 instance

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
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/pip/", line 215, in main
status =, args)
File "/usr/local/lib/python2.7/site-packages/pip/commands/", line 342, in run
File "/usr/local/lib/python2.7/site-packages/pip/req/", line 784, in install
File "/usr/local/lib/python2.7/site-packages/pip/req/", 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/", line 1064, in move_wheel_files
File "/usr/local/lib/python2.7/site-packages/pip/", line 345, in move_wheel_files
clobber(source, lib_dir, True)
File "/usr/local/lib/python2.7/site-packages/pip/", line 316, in clobber
File "/usr/local/lib/python2.7/site-packages/pip/utils/", line 83, in ensure_dir
File "/usr/lib64/python2.7/", 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
[ec2-user@ip-172-31-28-178 ug]$ which pip
[ec2-user@ip-172-31-28-178 ug]$

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download