Check Python version using Python Script
In this article, we will learn the command to check the version of Python using Python script. Let's first have a quick look over what is a Python script.
Python is a well known high-level programming language. The Python script is basically a file containing code written in Python. The file containing the python script has the extension
‘.py’ or can also have the extension
‘.pyw’ if it is being run on a windows machine. To run a python script, we need a python interpreter that needs to be downloaded and installed.
Check Python Version
Like much other software, Python too has several different versions released to date. After Python or any installation, one should check the version of the software that is running the script. Python uses
version keywords to check the Python version. The python script is a simple code that can be run in different modes such as using a Python interpreter, on a command line, or using any Python editor. Let us look over the commands to check the Python version.
There are different versions of Python, but the two most popular ones are Python 2 and Python 3. When looking at the version number, there are usually three digits to read:
- the major version
- the minor version
- the micro version
Let us look over the commands to check the Python version.
Check Python Version in Linux
Most modern Linux distributions come with Python pre-installed. To check the version installed, open the Linux terminal window and enter the given command:
Check Python Version in Windows
Most out-of-the-box Windows installations do not come with Python pre-installed. However, it is always a good practice to check the version. To check the version installed, open Windows Powershell or any Python editor, and enter the given command:
Check Python Version in MacOS
If you are using a macOS, check the Python version by entering the given command in the terminal:
In all the three version checks, you will an output somewhat like Python 3.5.2. As discussed above 3 is the major version, 5 is the minor version and 2 is the micro version.
Use sys module to check Python version
import sys print("Python version") print (sys.version) print("Version info.") print (sys.version_info)
3.5.2 (default, Sep 10 2016, 08:21:44)
[GCC 5.4.0 20160609]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
Use platform module to check Python version
from platform import python_version print(python_version())
In this article, we learned to use a simple command
python --version to check the version of Python on any Python script whether it is an editor, interpreter, or a command line. We saw the other two ways i.e.
python_version to check the Python version.