Kakashi
Kakashi

Reputation: 355

How to change a python path in sudo state?

My problem is that when I do :

$ which python => I get /a/b/c/python as my directory

but if I do $ sudo which python => I get /d/e/python as the result

How do I change the sudo one to match with the normal case, it is making it impossible to install libraries from source.

Upvotes: 3

Views: 3761

Answers (3)

str8
str8

Reputation: 242

It uses the first one found in $PATH

try doing

echo $PATH

then

sudo bash -c 'echo $PATH'

I bet these are different.

In any case, there is usually an rc script of some sort for the shell you use in both /root and your current user, just rearrange the paths in the environment variable for the one you want.

Upvotes: 0

Rob Russell
Rob Russell

Reputation: 472

According to https://askubuntu.com/questions/477987/two-python-distributions-sudo-picking-the-wrong-one this is a result of secure_path (specified in /etc/sudoers) overriding your normal PATH.

I've worked around it by giving the path to the path to the binary I want to run. For example:

$ which pip
/opt/local/bin/pip
$ sudo /opt/local/bin/pip install foo

It's not ideal but it works and doesn't subvert secure_path.

Upvotes: 1

ruakh
ruakh

Reputation: 183280

I would first try this:

sudo -i which python

which (indirectly) causes the root user's profile to be run, including any non-default configuration of the path. (By default, sudo doesn't bother with that.)

If that doesn't work, then that tells you that /usr/local/bin isn't in the path set up by the root user's profile (or isn't before /usr/bin), so your options are either to change the root user's profile and use the above, or else to use:

sudo -E which python

to preserve your path (and the rest of your environment). This may be less secure.

For full details on each of these options, see the sudo man-page.

Upvotes: 0

Related Questions