How to change from default to alternative Python version on Amazon, RedHat, Ubuntu or Debian Linux

If you want to change your python version on the fly and without breaking our codes, you can use this article to know how to do it for your session, or even automate your scripts.

The Linux alternatives command works setting a priority list for the desired application.

You can set a lower priority for less used executables and higher for most used. Now let’s go for the examples:

First, check which python versions you have installed:

ls /usr/bin | grep python
python
python2
python2.7
python2.7-config
python2-config
python3
python3.7
python3.7m
python-config

Now that you know which versions you’ve got, let’s consider that you want python command to respond as python2.7 or python3.7, where python3.7 is the most used. So we will give a higher priority for version 3.7 and lower priority for 2.7.

PS: For Debian, use update-alternatives command instead

To set alternatives and priorities, use the following:

sudo alternatives --install <destination_exacutable_path> <executable_name> <source_executable_path> <priority>

In our case:

sudo alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo alternatives --install /usr/bin/python python /usr/bin/python3.7 2

Now, enable auto-mode so the system can prioritize based on the recent created priority:

sudo alternatives --auto python

This way we have set higher priority to python3.7 which leads any python command to use this source. Let’s try:

python --version
Python 3.7.16

Well done! Now python3.7 is your main executable.

But how do I change it back to python2.7 or whenever version I want?

You have several ways, but I will list 4:

1 – Set python2.7 with higher priority instead of python3.7:

sudo alternatives --install /usr/bin/python python /usr/bin/python2.7 2
sudo alternatives --install /usr/bin/python python /usr/bin/python3.7 1
sudo alternatives --auto python
python --version
Python 2.7.18

To check how he decided to this result, issue a --display command:

sudo alternatives --display python
python - status is auto.
 link currently points to /usr/bin/python2.7
/usr/bin/python2.7 - priority 2
/usr/bin/python3.7 - priority 1
Current `best' version is /usr/bin/python2.7.

2 – Remove the alternatives and recreate them:

sudo alternatives --remove python /usr/bin/python2.7
sudo alternatives --remove python /usr/bin/python3.7
sudo alternatives --install /usr/bin/python python /usr/bin/python2.7 2
sudo alternatives --install /usr/bin/python python /usr/bin/python3.7 1
python --version
Python 2.7.18

3 – Set it manually and interactive with --config flag:

sudo alternatives --config python
There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/bin/python3.7

Enter to keep the current selection[+], or type selection number: 1
python --version
Python 2.7.18

4 – Set manually without interaction:

sudo alternatives --set python /usr/bin/python2.7
python --version
Python 2.7.18

And remember: Any change in alternatives, disable auto mode. If you want to always use auto, you should enable it after each intervention.

That’s it! I hope this makes your life easier đŸ˜‰

Extras:

In the back, alternatives write a file called: /var/lib/alternatives/python

So see it’s contents, just cat /var/lib/alternatives/python and you will see something like this:

manual
/usr/bin/python

/usr/bin/python2.7
1
/usr/bin/python3.7
2

This is also useful to check which other commands are making use of alternatives:

ls -larh /var/lib/alternatives/
total 28K
-rw-r--r--  1 root root   66 Jul 25 14:38 python
-rw-r--r--  1 root root  689 Apr 20 18:29 mta
-rw-r--r--  1 root root  101 Apr 20 18:28 libnssckbi.so
-rw-r--r--  1 root root   57 Apr 20 18:28 ld
-rw-r--r--  1 root root  411 Apr 20 18:29 iptables
-rw-r--r--  1 root root   78 Apr 20 18:29 cifs-idmap-plugin
drwxr-xr-x 31 root root 4.0K May  8 20:42 ..
drwxr-xr-x  2 root root  103 Jul 25 14:38 .

Deixe um comentĂ¡rio

O seu endereço de e-mail nĂ£o serĂ¡ publicado. Campos obrigatĂ³rios sĂ£o marcados com *