sudo: python: command not found

Question:

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

Asked By: Phlox Midas

||

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

Answered By: Charles Duffy

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

Answered By: Mike

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

Answered By: thd

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

sudo python3 
Answered By: Xiang DAI

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.

Answered By: Dave
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.