How to Check and Print Python Version?

ItsMyCode |

In this tutorial, you will learn how to print the version of the current Python installation from the script.

Print Python Version using sys Module

The sys module comes as a built-in utility with Python installation, and to check the Python version, use sys.version property.

import sys

# Prints current Python version
print("Current version of Python is ", sys.version)

Output

Current version of Python is 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)]

If you are looking for details on the version, such as major, minor, type of release, etc., you can use *sys.version_info * property.

The *version_info * property prints the Python version in tuple format, which gives detailed information as shown below.

import sys

# Prints current Python version
print("Current version of Python is ", sys.version_info)

Output

Current version of Python is sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)

Print Python Version using platform Module

The other alternative way is to use a platform module in Python where you can leverage the built-in function platform.python_version() to print the current installed Python version in your machine.

import platform

# Prints current Python version
print("Current version of Python is ", platform.python_version())

Output

Current version of Python is 3.9.7

Print Python version using command line

If you don’t want to write any script but still want to check the current installed version of Python, then navigate to shell/command prompt and typepython --version

python --version

# Output 
# 3.9.7

20