Check Python Version

Check Python Version

Python is a versatile and widely used programming language, but different versions of Python can have varying features, syntax, and libraries. Knowing the exact version of Python installed on the system is crucial for compatibility and troubleshooting. This tutorial demonstrates how to check Python version.

1. 'version' option

The quickest way to check the Python version is by using the command line. Run the command with version option (or the -V shortcut) as follows:

python --version
python -V

On systems where Python 3 is installed alongside Python 2, you may need to use:

python3 --version
python3 -V

2. 'python_version' function

Python also provides built-in modules that allow you to check its version programmatically. The platform module is one such option:

import platform

version = platform.python_version()
print(version)

This method returns the Python version as a string in the X.Y.Z format.

3. 'version_info' components

For more detailed control or when you need individual version components (major, minor, or micro), use the version_info attribute from the sys module:

import sys

version = f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}'
print(version)

This approach allows you to retrieve each part of the version number separately.

Leave a Comment

Cancel reply

Your email address will not be published.