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 three.5.ten. Now if you lot you want to develop in Python three (which we recommend), you need to specify the version of Python yous are using (by running: 'python3 mycode.py'). This is annoying - this article tells you how to make Python iii your default Python (then you can run: 'python mycode.py').

You might be request, whether it'south maybe possible to have i version only? (and uninstall the previous version). The respond today is - No. In that location are system dependencies which need Python 2 as well. Until this changes, use the following recipe to go 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. Nosotros are taking but the function useful for our makerspace activities ('Modify Python version System-wide'), click the link to come across more options.

Setup Default Python Version

Python Banner

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

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

find current python versions

  • We will employ the update-alternatives command. Starting time, nosotros will list the available alternatives (most probably, you will get an empty list equally seen in the screenshot below)
          sudo update-alternatives --list python                  

list python alternatives

  • Update the alternatives table to include both Python two.7.x and Python iii.5.x. Make sure to substitute the versions of Python in the command beneath with the versions of your organisation, which you take noted in the first pace. Notice we utilise only the first 2 digits of the version (i.e. we ignore the 3rd version number office)
          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 cease up with output similar to this.

update alternatives table for python

The --install choice take multiple arguments from which it will be able to create a symbolic link. The concluding statement specified is priority. That means, if no transmission culling selection is made the alternative with the highest priority number will be set.

  • Bank check the consequence. Type the following:
          python --version                  

Yous should run into Python 3 as preferred version (due to the priority setting in the priorities table)

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

          update-alternatives --list python                  

validate priorities

Switching default Python versions

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

          sudo update-alternatives --config python                  

choosing python versions

Cleanup

One twenty-four hours, your Python version(s) volition change. To remove an entry from the alternatives table, simply type something like this (nosotros will remove the table entry for Python three.5 in this case):

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

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

  • How to modify from default to culling Python version on Debian Linux