switch python versions in ubuntu

How to Switch Python Version in Ubuntu/Debian

Some system administrators and developers maintain multiple python versions on their systems, for the purpose of development and testing. But it is important to use the appropriate python version for your application, otherwise it may not run properly. You can always change python version to be used for your script or application, using update-alternatives tool. In this article, we will learn how to switch python version in Ubuntu/Debian.


How to Switch Python Version in Ubuntu/Debian

Here are the steps to switch python version in Ubuntu/Debian. We will use update-alternatives tool for this purpose. It is already installed on all Ubuntu/Debian systems, by default. It allows you to easily create & maintain symbolic links to various commands on your system. For our example, we will switch between python2.7 and python3.9. We will create symbolic links and groups for both the python versions.

We will assume that python2.7 is installed at /usr/bin/python2.7 and python3.9 is installed at /usr/bin/python3.9.


1. Create Symlink and Group for Python 2.7

Open terminal and run the following command to create a symbolic link and priority 1 for python 2.7.

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 

Please note 1 at the end of above command, used to assign option number 1 to python 2.7


2. Create Symlink and Group for Python 3.9

Similarly, run the following command to create a symbolic link and option number 2 for python 3.9.

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2 

Repeat the above step if you have any more python versions on your system.


3. Switch Python Version

At this point, there are 2 python versions configured in group ‘python’. You can easily switch between the two with the following command.

$ sudo update-alternatives --config python

You will see the following output.

Output: [Select on option] 

There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.9   2         auto mode
* 1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.9   2         manual mode

Press  to keep the current choice[*], or type selection number: 2

You will see a list of all python versions configured using update-alternatives, on your system. Enter the desired option 1 or 2 to switch to python 2.7 or 3.9 respectively. The * against selection 1 indicates it is the current default version. If you want to switch to python 3.9, you need to enter 0 or 2.

Once you enter your choice, update-alternatives will switch default python version on your system. In other words, when you call python command on your system, update-alternatives will point it to v3.9.

Now if you check the default python version with the following command, you will see that it has switched to python 3.9.

$ python -V 
Python 3.9.2

In this article, we have learnt how to switch python version in Ubuntu/Debian. You can use this method to manage multiple python versions on your system.

Also read:

How to Disable HTTP Methods in Apache
MySQL Datetime vs Timestamp
How to Remove Line from File Using Python
How to Combine Multiple CSV Files Using Python
How to Rename Multiple Files in Directory With Python

Leave a Reply

Your email address will not be published. Required fields are marked *