How To Check Python Version?

Python is one of the most popular programming and scripting languages. Python is used for different types of applications from website to machine learning, big data to simulation. Python is generally installed in a lot of computers and systems. But Python is a dynamic language that is updated regularly with new features.

When a Python application works with a specific version it may not work with another version. In this tutorial, we will learn how to print or display the python version.

Python Versions (Python 2 and Python 3)

As a dynamic language Python updated regularly and it has a lot of versions. Every Python release is versioned like below.

MAJOR.MINOR.MICRO
  • MAJOR is the major version number of Python which can be 2 or 3 but the 2 will be absolute in the future.
  • MINOR is the minor version number which is increased not so regularly.
  • MICRO is the version number that changes regularly.

Display Python Version From Command Line

In order to run python applications and script the binary Python interpreter is used. Python interpreter is a binary file which is generally named as python or python2 or python3. This can be changed according to the installation and situation. These binaries or commands provide the –version option which will print the complete python version from the command line. This command line can be a bash terminal or MS-DOS or PowerShell.

Generally Python3 binary is named as python3 andPython2 binary is named as python or python2. In generally following commands can be tried to get exact Python version.

$ python --version
$ python2 --version
$ python3 --version

The output will be like below according to the Python version.

Python 3.8.6

Python is develeoped for 20 years and it is planned to end the Python2 support at the end of the 2020. Today most of the popular Python2 3rd party modules are ported into the Python3.

Display Python Version From Script or Interactive Interpreter

Python provides the sys module in order to get system-related information where Python version number is also provided The sys.version_info provides the complete Python version with major, minor, micro, releaselevel, and serial information.

import sys

print("Python Version Number:",sys.version_info)

print("Python Major Version:",sys.version_info.major)

print("Python Minor Version:",sys.version_info.minor)

print("Python Micro Version:",sys.version_info.micro)

The output will be like below.

Python Version Number: sys.version_info(major=3, minor=8, micro=6, releaselevel='final', serial=0)
Python Major Version: 3
Python Minor Version: 8
Python Micro Version: 6

Display Python Version From Linux Apt or Apt-get Package Managers

If you are using a Linux system like Ubuntu, Debian, Mint and Kali and installed Python with the apt or apt-get package managers the installed Python version can be displayed with the package information.

$ apt show python3
Display Python Version From Linux Apt or Apt-get Package Managers

Display Python Version From Linux Yum and Dnf Package Managers

If you are using Linux distribution like Fedora, CentOS, RHEL and installed Python with package managers like yum or dnf you can show the package information to display Python version. The yum info or dnf info command can be used for this.

$ dnf info python3
Display Python Version From Linux Yum and Dnf Package Managers

Display Python Version with pip Command

The Python Package Manager (pip) can be also used to display the installed Python version. As pip is integrated with Python the “pip -V” or “pip –version” commands will list pip and Python version like below. but only the Major and Minor version numbers will be listed the micro version number will not display with this pip command.

$ pip -V
pip 20.1.1 from /usr/lib/python3/dist-packages/pip (python 3.8)
 
$ pip --version
pip 20.1.1 from /usr/lib/python3/dist-packages/pip (python 3.8)

Leave a Comment