How to Check Python Version in Linux
Table of Contents
The knowledge of the correct version of python is important for many tasks. For example, we may want to install a library that requires a specific version of python to check if our system has that version already installed. For example, in the Terminal formatting tutorial using rich, we have learned about a library called rich, which is used for terminal formatting in python. Still, the library requires Python 3.6.1 or later installed in the system.
This tutorial will see how to check the python version running on your Linux system. Many Linux systems also have multiple Python versions installed; we will also see how to know which versions are installed and how to use a particular version.
Python uses the semantic versioning technique to give a version to its new release. The Python versioning contains three numbers to identify each release uniquely. The production-ready releases follow the below-shown syntax for versioning.
For example, In Python Version 3.8.6, 3 is the major release(also known as python3), 8 is the minor release number, and 6 is the micro release number. The below list shows some more details about them.
- MAJOR: The Python programming language has two major releases that are not fully compatible with each other. The major version has many subversions. For example, python 3 has some subversions like 3.8.3, 3.4.5, etc.
- MINOR: These releases are bringing new functions, classes, features, etc. For example, python 3.7.x, python 3.8.x, etc.
- MICRO: The python micro releases contains many bug fixes and improvements.
The python micro and minor releases are somewhat compatible, and a new version can run the same code without much problem. Still, the major releases were not compatible with each other. If we write software using Python2, it will have some problems while running using the python3 interpreter.
Check all Python Versions Installed in the System
Many Linux and Mac Operating Systems come preinstalled with Python. Since all the Linux programs do not use the same python version, there are many python versions pre-installed with Linux. In Linux, the python binaries were installed in the directories /usr/bin/ or /usr/local/bin/. We can check which python version is installed in our system by typing the following command in the Linux terminal.
We will get the names of all the files starting with python present in the /usr/bin/ directory. I got the following output while running the command in my system. I have five different versions of python binaries installed in the path /usr/bin/. They are python, python2, python2.7, python3, python3.8. You may get some other output depending on the python installation in your system.
How to run code with a specific python version
To run a python script using a custom python binary, use the binary file name in the command line following the python script name. See the below command for illustration.
We can also run the python script using python 2.7 by running the following command.
Checking Python Version
There are two ways to check the python version; the first way is the simplest by using the command line, while the second way is by using writing a python script. Let us discuss both the ways in detail.
Using Command Line
We can check the version of python by using the command line. To display python’s version, run the python binary file following the –version or -V command. See the below examples for illustration.
Using the v parameter:
Using the –version parameter:
Using Python Script
We can also use a python script to check which python version is used to run the script. This technique has a benefit if our python program can be run only in a specified version. For example, assume you have written a python code that used f-strings for formatting strings. F-strings have been included in python in the 3.6 release, and so the code will work only on python version 3.6 or later.
We can improve the program by checking the python version used programmatically and using the if/else condition to check whether f-strings are supported by the version. If f-string is supported, then continue with the code; else, run some other code block.
We have many modules that can help us to get the version of python. In this tutorial, we were using the sys and platform library. These libraries are present in the python standard library, so we have no installation problem for these libraries.
Using the sys library
To get the version number using the sys library, we have two methods. The first uses the sys.version string, which contains some details like the python version, compiler version, etc. The second one uses the sys.version_info, which contains a tuple containing some python version info. The below code shows a simple use of the sys.version to check the version.
# importing the required modules import sys # using the version string of # the sys module print(sys.version)
In the above output, we can see that we have got the python version and the GCC compiler version used to run the code.
We can also use the sys.version_info, which is a tuple containing the version information. The below example code shows a simple use of it.
# importing the required modules import sys # using the version_info tuple of # the sys module print(sys.version_info)
From the above output, we can see that the python tuple contains five values. The first three values are the major, minor, and micro version number that we required.
Using the platform library
We can also use the python platform library to get the python version used to run the code. To get the version number using the platform library of python, use the python_version() function of the platform module. The python_version() function will return a python string containing the python version used for running the code. The following code shows a practical illustration of getting the python version using the platform module.
# importing the required modules import platform # using the python_version()_function of # the platform module print(platform.python_version())
There is also a python_version_tuple() function in the platform module, which can get the python version in a tuple. See the below code for illustration.
# importing the required modules import platform # using the python_version()_function of # the platform module print(platform.python_version_tuple())
From the above output, we can see that the python_version_tuple() function returns a tuple containing each of the major, minor, and micro version numbers in a tuple.
In this tutorial, we have learned about the versioning of Python. We have also learned how to get the python version using both the command line and the python script. You may also want to see our guide on getting started with python covering all the basics of python in a simple way.