Python is a staple found frequently on most Linux systems, including a Raspberry Pi. It comes installed by default with Raspberry Pi OS, as many programs and applications commonly rely on it, since they are programmed in the Python language. The version of Python will determine which features it comes with, as the developers are always adding new features and patches to it.
Depending on what program you are trying to run, it may require a newer version of Python than you have access to on your Raspberry Pi. Knowing which version of Python you have installed will clue you into what programs you are able to run. In this tutorial, you will see how to check the Python version on a Raspberry Pi.
In this tutorial you will learn:
- How to check installed Python executable versions
- How to check Python version via command line
- How to check Python version via interpreter and Python script
|Category||Requirements, Conventions or Software Version Used|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to check Python version 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*
- 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
- We can also directly use the Python interpreter to check the Python version:
$ python3 OR $ python2
And then type…
>>> import platform >>> platform.python_version()
- And in case you want to check the Python version from a Python script. Paste the following contents into
import platform python_version=platform.python_version() print (python_version)
And then execute the script:
$ python3 check-python-version.py OR $ python2 check-python-version.py
In this tutorial, we saw how to check 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 check the Python version via command line, Python interpreter, or a Python script on your Raspberry Pi.