Two python distributions, sudo picking the wrong one

5,355

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.

Share:
5,355

Related videos on Youtube

Hans
Author by

Hans

Things change, currently interested in Node.js and Meteor. Like cycling as well as cultivating code and crops.

Updated on September 18, 2022

Comments

  • Hans
    Hans over 1 year

    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.

  • jobin
    jobin almost 10 years
    Calling your anaconda python binary is your safest bet.