DEV Community

Srinivas for ItsMyCode

Posted on • Originally published at itsmycode.com on

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)
Enter fullscreen mode Exit fullscreen mode

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)]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output

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

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())
Enter fullscreen mode Exit fullscreen mode

Output

Current version of Python is 3.9.7
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The post How to Check and Print Python Version? appeared first on ItsMyCode.

Discussion (0)