howtouselinux

3 Ways to Check Python Version in Linux

Table of Contents

How to get the Python version on Linux is a commonly asked question during a Linux job interview. In this article, we will cover 3 ways to find the Python version in Linux.

We will learn how to check the python version using the python command as well as how to determine the python version programmatically, from the python console and using python script.

Understanding Python Version

Python is a high-level, general-purpose programming language created by Guido van Rossum. It was first released in 1991. Generally, Linux based distros have pre-installed Python version.

Before we can check what version of Python our computer has loaded, we must understand the version scheme of Python. Every Python version has three digits.

The first digit represents the major version, the second the minor version, and the third digit represents the micro version or “revision level.”

For example, in Python 3.6.8, 3 is a major version, 6 is a minor version, and 8 is a micro version.

We must also note that major versions are typically not fully compatible with each other. In other words, software written with Python version 2.x.x may not run correctly with Python 3.x.x. However, minor releases are typically compatible with the previous releases. For example, code written in Python 3.1.x will run with Python 3.10.x (which is the current Python version).

Python Version release

Python 3.0, a major, backwards-incompatible release, was released on December 3, 2008 after a long period of testing. Many of its major features have also been backported to the backwards-compatible, though now-unsupported, Python 2.6 and 2.7.

3 Ways to check Python version in Linux

The following commands can be used to get Python version in Linux.

  • python3 –version or python -V or python -VV
  • python3 -c “import sys; print(sys.version)”
  • python3 -c “import sys; print(sys.version_info)”
  • python3 -c “import platform; print(platform. python_version())”

 

Check Python version in Linux with python -V command

To check Python version in Linux,  you can use  python -V command. All you need is to open the terminal then type python -V in the prompt. The Python version will be listed.  In some environments, the Python2.x series is assigned to python command, and the Python3.x series is assigned to python3 command.

  • $ python –version
    Python 2.7.15
  • $ python -V
    Python 2.7.15
  • $ python3 –version
    Python 3.7.0
  • $ python3 -V
    Python 3.7.0

Check Python version with sys module in Linux

The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. We can use sys.version to return a string containing the version of Python Interpreter with some additional information.

$ python3 -c “import sys; print(sys.version)”
3.6.8 (default, Mar 18 2021, 08:58:41)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]

$ python3 -c “import sys; print(sys.version_info)”
sys.version_info(major=3, minor=6, micro=8, releaselevel=’final’, serial=0)

 

Check Python version with platform module in Linux

The platform module in Python is used to access the underlying platform’s data, such as, hardware, operating system, and interpreter version information.

platform.python_version() returns a string major.minor.patchlevel. It is useful when we want to get the version number as a simple string.

$ python3 -c “import platform; print(platform.python_version())”
3.6.8

$ python3 -c “import platform; print(platform.python_version_tuple())”
(‘3’, ‘6’, ‘8’)

We can also get more info with Python platform module.

  • platform.architecture()
    returns information about the bit architecture
  • platform.machine()
    returns the machine type, e.g. ‘i386’.
  • platform.node()
    returns the computer’s network name (may not be fully qualified!)
  • platform.platform()
    returns a single string identifying the underlying platform with as much useful
    information as possible.
David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.