Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sudo: python: command not found

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

like image 546
Phlox Midas Avatar asked Jun 23 '17 16:06

Phlox Midas


2 Answers

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).

like image 83
Charles Duffy Avatar answered Sep 20 '22 13:09

Charles Duffy


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

like image 36
v4r Avatar answered Sep 17 '22 13:09

v4r