Psychz - Sandip
Votes: 0Posted On: Jun 30, 2021 11:12:51
Python is an object-oriented, interpreted, high-level programming language with dynamic semantics. Python's simple, easy-to-learn syntax emphasizes readability and therefore reduces the cost of program maintenance. It supports modules and packages, which encourages program modularity and code reuse. There are different Python versions, but the two most popular ones are Python 2.7.x and Python 3.7.x. The x stands for the revision level and could change as new releases come out.
When looking at the version number, there are usually three digits to read:
major version (X.0.0)
minor version (0.X.0)
micro version (0.0.X)
While major releases are not fully compatible, minor releases generally are. Version 3.6.1 should be compatible with 3.7.1, for example. The final digit signifies the latest patches and updates.
When using Python, it is essential to know which version an application requires and which version you have. Today, there are two active versions of Python: Python 2 and Python 3, of which Python 2 is no longer supported from January 2020.
To verify the version of Python3, please use the following command.
[root@centos-8 /]# python3 --version
Output
Python 3.6.8
Check Python Version in Script
When writing an application, it is helpful to have the software check the version of Python before it runs to prevent crashes and incompatibilities.
Use the following code snippet to check for the correct version of Python:
import sys
if not sys.version_info.major == 3 and sys.version_info.minor >= 6:
print("Python 3.6 or higher is required.")
print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
sys.exit(1)
When this script runs, it will test to see if Python 3.6 is installed on the system. If not, it will send a notification and displays the current Python version.
Conclusion
We hope you have a good understanding of how to check for Python installed in several different operating systems. Python is a powerful programming language. Thus it's essential to understand its various versions. Please vote us if you like this article.