Using update-alternatives to avoid /usr/bin/python: bad interpreter and point python to python3

Written by - 0 comments

Published on - Listed in Linux Python

When running a system with only python3, it may happen that /usr/bin/python does not exist.

Yet most of the python scripts use this path (or env path) to find python:

root@linux:/usr/lib/nagios/plugins# head -1

Trying to execute such a Python script then fails:

root@linux:/usr/lib/nagios/plugins# ./
-bash: ./ /usr/bin/python: bad interpreter: No such file or directory

On this machine, a Debian 11 (Bullseye), we can see python3 exists:

root@linux:~# ls -la /usr/bin/python3
lrwxrwxrwx 1 root root 9 Apr  5  2021 /usr/bin/python3 -> python3.9

But, as mentioned, no /usr/bin/python.

There are now two possible fixes:

a) Adjust the Python script and point the first line to /usr/bin/python3. No, we won't do that!

b) Tell the system that /usr/bin/python should point to /usr/bin/python3.

Of course, this could be done manually with a symbolic link, but a better way is to use update-alternatives (a fancy, more descriptive and dynamic symlink).

root@linux:~# update-alternatives --install /usr/bin/python python /usr/bin/python3 1
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

In the background, update-alternatives created a symlink /usr/bin/python:

root@linux:~# ls -la /usr/bin/python
lrwxrwxrwx 1 root root 24 Mar 16 14:41 /usr/bin/python -> /etc/alternatives/python

As we can see, this points to /etc/alternatives/python. Let's see what is hidden there:

root@linux:~# ls -la /etc/alternatives/python
lrwxrwxrwx 1 root root 16 Mar 16 14:41 /etc/alternatives/python -> /usr/bin/python3

Ah, it's yet another symlink! Told you, it's a symlink, but fancier. :-)

We can now use update-alternatives again to find a list of alternative programs:

root@linux:~# update-alternatives --list python

Of course update-alternatives can be used for all kinds of programs and scripts, not just Python. Another often used update-alternatives combo is php or cmake with update-alternatives.

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.