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
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).
If you don't want to modify your bashrc, you can always do this: 
sudo env "PATH=$PATH" python something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With