Learn Python – How to Check Python version- Basic and advance

The Python interpreter is commercially used throughout many industries for source coding, computer programming, and code testing. It takes the commands from the user and executes them after interpreting them. Hence, it becomes very important to know about the version of Python interpreter that we are currently using.

As we know that it is the interpreter which takes the code from the user and executes it line by line. Sometimes, suppose we have an older version of the Python interpreter. In that case, it makes the command doesn’t execute appropriately because of the older version of the interpreter and its incompatibility with the new functions used in the command.

As we all know that Python is a programming language that comes with a regular update that adds some new features and functions with it; therefore, as a user, we also have to regularly update our Python interpreter with the latest released version of it.

Note: The latest version of the Python interpreter as of now is ‘3.9.6’ with a stable version.

Benefits of checking Python interpreter’s version:

Following are some highlighted benefits of checking the version of Python interpreter we are using:

We will know if we are missing any update for the interpreter or the updates in function comes with it.

We can find out if the syntax error is producing because of an older version of the Python interpreter.

We can have access to all the latest functions and updates that come with the latest version of the Python interpreter.

Checking the Version of the Python interpreter

In this section, we will learn about the methods by which we can quickly check the version of the Python interpreter we are using. We can even check the Python interpreter version using a Python program. We will also use the command prompt method to check the Python interpreter version we are using.

Following are the methods for checking the version of the Python interpreter that we are going to learn in this tutorial:

With python_version() function

With python -V command

With sys.version method

Let’s understand that how we can use these methods in Python as well as in the command prompt terminal to check the Python interpreter’s version.

Method 1: Using python_version() function:

To use this function for checking the version of the Python interpreter, we first have to import the platform library. The python_version() function always returns the version of an interpreter in the string format. To better understand about working of this, let’s use this in our Python program.

Look at the following program:

Example –

# Importing platform library  
from platform import python_version   
# Getting Python interpreter version as a result  
print("Current Version of Python interpreter we are using-", python_version())  

Output:

Current Version of Python interpreter we are using- 3.9.0

Explanation –

As we can see in the above output, we are using the ‘3.9.0’ version of the Python interpreter.

We can also use this function in the command prompt terminal and check the version of the interpreter. For using the python_version() function in the terminal, we have to adhere to the following steps:

Step 1: Open the cmd terminal shell of the system.

Step 2: Write ‘python’ and press the enter key to enter into the Python shell.

Step 3: Now, write the above code line by line inside the terminal and then press the enter key.

The terminal will show the version of the interpreter as the output of the code as following:

Method 2: Using python -V command:

Using the ‘python -V’ command is considered the simplest and easiest method for checking the version of the Python interpreter we are using. It is an inbuilt command of the command prompt shell, and this method is specially built for the command prompt shell for checking the Python version.

In this method, we only have to follow the following two steps:

Step 1: Open the terminal shell present in our device.

Step 2: Write the following command in the shell and press enter:

'python -V'  

Now, we will get the version of the Python interpreter we are using in the string format.

Method 3: Using sys.version method:

To use sys.version method for checking the version of the Python interpreter, we first have to import the platform library. Like the python_version() function method, we can use this method both in command prompt shell as well as a Python program in Python shell. Here, we will only use this method as the Python program and get the version of the Python interpreter as the output of the program.

Look at the following program:

Example –

# Importing sys library  
import sys   
# Getting interpreter version as a result  
print("Current Version of Python interpreter we are using-" sys.version)  

Output:

Current Version of Python interpreter we are using- 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)]

So, as we can see in the output that we have got the version of the interpreter we are using in the string format, we have also got the tag and released date for the interpreter version in this sys.version method.