how to change default python version raspberry pi

How To Change The Default Python Version On Raspberry Pi

Python is an important element for a Raspberry Pi, with many projects relying on it. With new releases all the time, it can be tough to keep it updated, or at the right version for your applications. In this article, I will give you all the information to know the current version installed, install another one and change the default Python executable.

Several versions of Python can be installed on the same Raspberry Pi. A symbolic link is created to use the default version with the main command (/usr/bin/python). This link can be updated with another version if needed.

Let’s start by checking which versions are installed on your Raspberry Pi, and then how you can use a different one by default (or install a new one if needed).

By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only the essential concepts) required to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun parts. Get 10% off by downloading it today!

What is the default Python version on Raspberry Pi OS?

On the current release of Raspberry Pi OS (Bullseye), only Python 3 is installed by default. It means that using the “python” or “python3” command will do the same thing when executing a script.

At the time of writing, Raspberry Pi OS Bullseye comes with Python 3.9.2, but this is updated regularly in new images and system updates.

On the older version (Buster, or Legacy), Raspberry Pi OS comes with two versions:

  • Python 2.7.6, which is linked to the command /usr/bin/python (“python” in short).
  • Python 3.7.3, linked to /usr/bin/python3 (“python3”).
    It’s also this version that is being used in Thonny (a Python code editor preinstalled on Raspberry Pi OS).

It’s important to know which versions are installed on your system because you may need to install different libraries depending on it (read more in my other article about this), and not all Python projects are compatible with all Python versions.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now

So, let’s learn how to check the current version(s) of Python installed on your Raspberry Pi.

How to check the current Python version used on your system

The easiest way to see which Python version is installed on your system is to open a terminal and use the command:
python –version

So, in my example, I use:
python --version
It was on Raspberry Pi OS Bullseye, and I know that it’s using 3.9.2 by default when I use the command “python” to run a script.

You may also have the command “python2” or “python3” on your system, you can use the same option (–version), to check which one is used in each case.
This was my result on Raspberry Pi OS Legacy, for example:

If you use an editor instead of the terminal to run your scripts, it may use a different version, so make sure you check it too.
For example, Thonny is set by default to use Python 3, even on the Legacy version of Raspberry Pi OS. As mentioned earlier, it can cause issues if you are installing additional libraries for Python 2 and try to use them in Thonny.

Are you a bit lost in the Linux command line? Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

How to change the default Python version on Raspberry Pi OS

There are three different ways to change the default Python version used on Raspberry Pi OS:

  • Use the command for a specific version (“python2”, “python3”, etc.).
  • Change the default version for the command “python” system-wide.
  • Or update the default version in your text editor.

Let’s see how to do this in each case.

Specify the Python version in the command line

All the Python executables are stored under /usr/bin on Raspberry Pi OS and most Linux distributions. When running a script in a terminal, it’s possible to specify the exact version you want to use directly in the command.

For example, you can use:
/usr/bin/python3.7 myscript.py
If Python 3.7 is installed, and you want your script to use this specific version.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now

You can use this command to see all the versions available on your system:
ls /usr/bin/python* -latr | grep -v config

Once you know this, you can just use the command corresponding to the version you want to use.
For example, I created a script that shows up the version used, and it will be something different depending on the command I use:

Change the default version system-wide

In a terminal, shortcuts are created for you. For example, the “python” command is only a symbolic link to “/usr/bin/python3.9”, it’s not a standard executable. The link can be updated to fit your needs.

If you re-check my previous screenshot, you’ll see that “python2.7” is a real file, but “python” is only a link to that real file (there is an arrow in the filename column).

If you want “python” to use the latest version instead, for all users on your system, you can simply update that link with:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.7 /usr/bin/python

As you can see in this screenshot, the Python version used in my terminal when I just type “python” is now updated. This simple tip will make your life easier with all your coding projects, especially if you don’t need to run anything in an older version of Python.

💰 Make Money Sharing Your Raspberry Pi Expertise!
Help others navigate the world of Raspberry Pi with your insights.
Become a RaspberryTips Contributor!

Change the default version in Thonny

Thonny doesn’t rely on the symbolic link to the Python executable for the version used. It’s set in the text editor options and can be updated to any version of Python available on the system.

When you open Thonny, you can see the current version used in the Shell part of the app.
Here is how to use a different version:

  • Click on “Switch to regular mode” in the top-right corner.
  • Restart the application.
    You should get access to the full menu instead of the simplified version with only the main icons.
  • Then in the main menu, go to “Tools” > “Options”.
  • Open the “Interpreter” tab.
    There, you can choose a different executable.

Note: Currently, Thonny only supports Python versions above 3.5. So, you can’t use this method for a downgrade. Try another programming editor in this case (here are my favorites), or use the terminal to run your script (you can still create them in Thonny, just not run them).

Well, we already covered a lot in this article, I hope it answers your questions.
The last thing I want to briefly mention is how to install and use a different version of Python than the ones preinstalled on your system.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now

🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

How to install a different Python version on Raspberry Pi OS

All Python versions are available on the official website and can be installed on a Raspberry Pi from their source code. It takes a while to build and install them, but it’s not complicated.

I have an entire guide on how to install the latest Python version on Raspberry Pi here.
If you want to give it a try, I highly recommend reading it first.
But here is the short version:

  • Open the official website, and find the link to the version you need.
  • Download the version you would like to install.
    You can either use your web browser if you have a GUI or use the “wget” command. For example:
    wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
    Use the link you got from the website, not my example.
  • Then extract the archive with:
    tar -zxvf Python-3.11.0.tgz
    Not sure what tar is? Check this article: How To Use the Tar Command on Linux.
  • Go to the new folder:
    cd Python-3.11.0
  • Run the configuration command:
    ./configure --enable-optimizations
  • And finally, start the installation with:
    sudo make altinstall

It will take a while, probably between 10 and 30 minutes depending on your Raspberry Pi model.
It won’t replace any link on your system (hence the “altinstall” part of the command), so you need to follow the previous section after the installation if you want to use this new version system-wide.

It works the same way for a “downgrade”. You can download an older version, compatible with your application, install it, and set it by default if needed.

Once done, you should be ready to experiment with any Python project you like, here are a few suggestions for you:

Whenever you’re ready, here are other ways I can help you:

The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts