How to Check Python Version

Using certain utilities and operators requires newer versions of Python and some modules/packages will only work on particular Python versions. It is therefore important to know what release of Python your system is running before writing code and installing packages.

 

In this tutorial, we will learn how to check the Python version actually executing scripts in several different ways.

 

From the Command-line

The easiest way to check the version of Python installed on your system is VIA the command-line. This is done by typing python followed by -V, or --version.

 

python -V
Python 2.7.12

 

For python 3 use:

 

python3 -V
Python 3.7.9

 

Show Version From Within Python

We can get the version of Python from within a Python script by using the sys package. Import it then use the sys.version object.

 

import sys

print(sys.version)
3.8.5 (default, Sep  4 2020, 02:22:02)
[Clang 10.0.0 ]

 

It is possible to get system info in a Python program as a tuple by using sys.version_info.

 

import sys

print(sys.version_info)info inside
sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)

 

Each tuple element can be accessed by its index. Let's say we want to get the system release level we could do this:

 

import sys

print(sys.version_info[3])
final

 

The same data can be obtained by specifying the name of the tuple element, which is more readable.

 

import sys

print(sys.version_info.releaselevel)
final

 

Get Full Python Version Number in a Script

To get the full Python version number from in a Python script, use the platform package. Import it then call the python_version() method.

 

import platform

print(platform.python_version())
3.8.5

 

Conclusion

You now know how to get what version of Python is running from the command-line and from within a Python script.

terminal version