I'm back to Linux after an over 10 year abstinence (fool me thinks). And a little rusty in the sys admin department.

I'm faced with an issue with my python distribution. I'm using Python 2.7, but based on the Anaconda flavour. I followed the standard guidance but recently I discovered an issue that I'm not sure how to fix.

Under sudo, the standard Python as comes with Ubuntu is provided. Under my user account python points to the Anaconda version:

dhk@localhost:~/home/$which python
/opt/anaconda/bin/python
dhk@localhost:~/home/$sudo which python
/usr/bin/python

This is an issue as using sudo pip [anything] usually acts on the wrong directory, yet I cannot use it without sudo.

up vote 2 down vote accepted

Sudoers specifies a secure_path:

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

This will override your local PATH, thus your virtualenv isn't used. You have a few options:

  • Call your python binary explicitly: sudo /opt/anaconda/bin/python file.py
  • Comment out the secure_path line in /etc/sudoers (but it's probably there for a reason)
  • Specify keep_env for this specific command (though this probably has similar issues to above).

Do not add your Python path to the secure_path as this will probably screw up more than a couple of standard sudo calls.

  • Calling your anaconda python binary is your safest bet. – jobin Jun 7 '14 at 9:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.