RasPi Banner

Configure Default Python version on your Pi

The Pi (Raspbian) comes pre-installed with 2 versions of Python. As of this writing, 2.7.x and 3.5.x. Now if you you want to develop in Python 3 (which we recommend), you need to specify the version of Python you are using (by running: 'python3 mycode.py'). This is annoying - this article tells you how to make Python 3 your default Python (so you can run: 'python mycode.py').

You might be asking, whether it's maybe possible to have 1 version only? (and uninstall the previous version). The answer today is - No. There are system dependencies which need Python 2 as well. Until this changes, use the following recipe to get Python setup the way you want to.

This guide is based and inspired from the following article by LinuxConfig: How to change from default to alternative Python version on Debian Linux. We are taking only the part useful for our makerspace activities ('Change Python version System-wide'), click the link to see more options.

Setup Default Python Version

Python Banner

  • First of all, check which versions of Python your system has. Just type the following:
python --version
python3 --version

You'll get something similar to this. Note the versions available (first 2 digits only)

find current python versions

  • We will use the update-alternatives command. First, we will list the available alternatives (most probably, you will get an empty list as seen in the screenshot below)
sudo update-alternatives --list python

list python alternatives

  • Update the alternatives table to include both Python 2.7.x and Python 3.5.x. Make sure to substitute the versions of Python in the command below with the versions of your system, which you have noted in the first step. Notice we use only the first 2 digits of the version (i.e. we ignore the 3rd version number part)
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2

If successful, you will end up with output similar to this.

update alternatives table for python

The --install option take multiple arguments from which it will be able to create a symbolic link. The last argument specified is priority. That means, if no manual alternative selection is made the alternative with the highest priority number will be set.

  • Check the result. Type the following:
python --version

You should see Python 3 as preferred version (due to the priority setting in the priorities table)

Similarly to above, you can also list the priorities table to confirm:

update-alternatives --list python

validate priorities

Switching default Python versions

If you have followed the guide above, you can from now on switch preferred Python versions anytime. Just invoke the command below and make your choice.

sudo update-alternatives --config python

choosing python versions

Cleanup

One day, your Python version(s) will change. To remove an entry from the alternatives table, simply type something like this (we will remove the table entry for Python 3.5 in this example):

sudo update-alternatives --remove python /usr/bin/python3.5

Always read more Python related topics on our main Python page.