19

I want to make sudo python find Python 3.

I had a strange issue where, in terminal, typing python --version gave 3.6 but sudo python --version gave 2.7. After trying a few things I finally uninstalled 2.7 with sudo apt-get purge python2*. That removed everything correctly. Still, I can't get sudo python to find Python 3.

I've tried changing my /root/.bashrc to have:

export PATH="/home/username/anaconda3/bin:$PATH"

and

alias python="/home/username/anaconda3/bin/python"

and I put the same lines in ~/.bashrc too.

My etc/sudoers has this line:

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

I've opened new terminals and even restarted the computer. Any ideas how to make sudo python just find Python 3? I don't want a single session fix but something that will work each time I use the terminal.

Thanks

8
  • 2
    sudo doesn't respect aliases. It can't, because an alias is a shell operation, but sudo is not part of the shell (can't access internal shell state), and directly calls the operating system's execve() call to invoke software being called. Commented Jun 23, 2017 at 16:48
  • That said -- sudo env PATH="$PATH" python should do the trick. Commented Jun 23, 2017 at 16:48
  • Which file would I have to put that in? Or would I have to type it each session? Commented Jun 23, 2017 at 16:50
  • Every time you run sudo python, but if you're doing that all the time... err... that's a smell. (Not quite a "code smell", necessarily, but let's call it a "bad-practice smell"). Commented Jun 23, 2017 at 16:50
  • that said, put psudo() { sudo env PATH="$PATH" "$@"; } in your .bashrc and call psudo instead of sudo, and there you are. Commented Jun 23, 2017 at 16:51

5 Answers 5

30

Your /etc/sudoers is explicitly configured to override your user's path with a known, secure one.

That said, if you want to always path the user's PATH through, you can easily override sudo with a function that will do this (installed in your ~/.bashrc or similar to make it persistent):

psudo() { sudo env PATH="$PATH" "$@"; } 

thereafter, psudo python will use the same python interpreter that would be found in the PATH.


If you really want to override the sudo command itself, that's doable too:

sudo() { command sudo env PATH="$PATH" "$@"; } 

The command builtin prevents the function from recursing (calling itself).

Sign up to request clarification or add additional context in comments.

1 Comment

That psudo solution is a great compromise. Thank you very much.
18

If you don't want to modify your bashrc, you can always do this: sudo env "PATH=$PATH" python something

Comments

8

The accepted answer suggests setting up functions to duplicate or replace sudo, with syntax new Linux user might find complex.

There is a simpler way...

User has miniconda3 python env:

(base) user@machine:~/$ which python
/home/user/miniconda3/bin/python
(base) user@machine:~/$ python --version
Python 3.9.12

sudo can not see python:

(base) user@machine:~/$ sudo which python
(base) user@machine:~/$ sudo python --version
sudo: python: command not found

Simply use "which python" in place of "python"!:

(base) user@machine:~/$ sudo `which python` --version
Python 3.9.12

This allows the shell interpreter to replace "python" with "/home/user/miniconda3/bin/python" in the sudo command.

Alternatively, set an environment variable, say PY to always use in place of python - this has the advantage of being usable inside shell scripts:

(base) user@machine:~/$ export PY=`which python`
(base) user@machine:~/$ $PY --version
Python 3.9.12
(base) user@machine:~/$ sudo $PY --version
Python 3.9.12

Note: sudo with --preserve-env=PATH is attractive, but does not work, because sudo uses secure_path from /etc/sudoers to look up executables, not $PATH.

4 Comments

This awesome answer should be the accepted one
Try this when your directory names contain spaces (not so unrealistic -- /Users/Some User/bin is perfectly legal).
@CharlesDuffy Yes, very common on Mac OSX.
This is the best practice
2

...other approach.

when I got to this post, I was just looking to run:

python -m spylon_kernel install

as I ran the command above, I got a message telling me to use sudo in addition to what I was typing, such as

sudo python -m spylon_kernel install

as I did it, I got the 'sudo: python: command not found' message from console, and adding --user such as:

python -m spylon_kernel install --user

was simply enough to get it done.

Notice that I did not use sudo command within the last command.

Comments

-2

If python 3.x is installed already, try the following code

sudo python3 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.