How to change default python version on Debian 9

How to change default python version on Debian 9

In this tutorial I will show you, how to change default python version on Debian 9 server.

Debian 9 Stretch comes with two 2.7 and 3.5 python versions. If you have not installed any python package yet you can choose between both by simply installing an appropriate package:

# apt install python

# apt install python3

Check Default Python Version

To check a default python version simply run python command and query its version:

$ python --version
Python 2.7.13

Install Python

Let’s assume that no python interpreter is not yet installed on our system. Hence, we can start by installation of both python versions:

# python --version
-bash: python: command not found
# apt install python python3

After installation the Python version 2.7 is made default:

$ python --version
Python 2.7.13

Update Python ALternatives List

To perform a system-wide switch between default python versions use update-alternatives command. At first the update-alternatives command will complain that there are no python alternatives available:

# update-alternatives --list python
update-alternatives: error: no alternatives for python

To install Python alternatives, first list all available options:

$ ls /usr/bin/python*
/usr/bin/python  /usr/bin/python2  /usr/bin/python2.7  /usr/bin/python3  /usr/bin/python3.5  /usr/bin/python3.5m  /usr/bin/python3m

Next, update the Python alternatives list for each version you whish to use. In our case with stick with /usr/bin/python2.7 and /usr/bin/python3.5 versions:

# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode

Please note that the integer number at the end of each command denotes a priority. Higher number means higher priority and as such the /usr/bin/python3.5 version was set in Auto Mode to be a default if no other selection is selected. After executing both above commands your current default python version is /usr/bin/python3.5 due to its higher priority (2):

# python --version
Python 3.5.3

Switch Between Python Versions

Now, that we have updated list of Python alternatives to perform a switch between any python version is to run:

# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

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

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode

and select an appropriate version using selction integer as shown above.

# python --version
Python 2.7.13

Local User Python Version

In case you need to only change a python version selectively on per user basis, you may try to edit user’s .bashrc file. For example to change to python version 3.5 execute the following commands:

$ python --version
Python 2.7.13
$ echo 'alias python="/usr/bin/python3.5"' >> ~/.bashrc
$ . .bashrc 
$ python --version
Python 3.5.3
Enjoyed this video?
How to change default python version on Debian 9
"No Thanks. Please Close This Box!"