It is possible to have multiple versions of Python installed on the Raspberry Pi simultaneously. It is also simple enough to switch between the versions, which may come in handy for Python programmers that need to support a mix of legacy and modern Python code. Or, perhaps you want to try out a new beta or nightly Python build, without replacing the more stable copy you already have. In this tutorial, you will see how to change between various Python versions on the Raspberry Pi.
In this tutorial you will learn:
- How to change between Python versions with
update-alternatives
on a Raspberry Pi

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Raspberry Pi |
Software | Python |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Change Python versions on Raspberry Pi step by step instructions
It is common for Raspberry Pi’s and other Linux systems to have multiple versions of Python installed. This is mainly to maintain backward compatibility with older programs that will only run on an older version of Python. The most common example would be having both Pyhton 2 and Python 3 installed.
- Your Raspberry Pi may have both Python 2 and Python 3 versions installed. We can list installed Python binary executables to see what version is installed on our Raspberry Pi:
$ ls /usr/bin/python*
Listing available Python executable versions on our Raspberry Pi - As we can see above, our Raspberry Pi has multiple versions of Python installed, both Python 2 and Python 3. We can get more precise version numbers by executing these commands:
$ python3 -V Python 3.9.2 $ python2 -V Python 2.7.18
- To switch among the available Python versions on our Raspberry Pi, we will use the
update-alternatives
command. This basically tells the system which executable file should be tied to thepython
command. First list all available python alternatives:
$ sudo update-alternatives --list python update-alternatives: error: no alternatives for python
- The above error message means that no Python alternatives have been recognized by the
update-alternatives
command. For this reason we need to update our alternatives table and include the Python versions that were revealed by the output in step 1:$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1 update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python (python) in auto mode $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
The
--install
option can take multiple arguments from which it will be able to create a symbolic link. The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python2.7 and as a result the /usr/bin/python3.9 was set as default Python version automatically by theupdate-alternatives
command. - Next, we can again list all Python alternatives:
$ sudo update-alternatives --list python /usr/bin/python2.7 /usr/bin/python3.9
- From now on, we can switch any time between the above listed Python alternative versions using below command and entering a selection number:
$ sudo update-alternatives --config python
Closing Thoughts
In this tutorial, we saw how to change between the installed versions of Python on a Raspberry Pi system. It is common to have multiple versions of Python installed on the system by default, including the latest version of Python 2 (now deprecated) alongside some version of Python 3. Now you will be able to change the Python version via the
update-alternatives
command on your Raspberry Pi.