Python

Different Ways to Check Python Version

Python is one of the most popular programming languages. In technical terms, it’s an interpreted, object-oriented, high-level programming language with dynamic semantics. It’s a relatively simple language. The unique syntax of Python focuses on readability. Python is open-source and available on all the major platforms.

As of now, Python 2 and Python 3 are the major releases that are still relevant. While Python 2 was marked obsolete, it’s still required for some codes to run. For the most part, however, having Python 3 is recommended. In this guide, we’ll check out various ways to check the version of Python installed in your system.

Prerequisites

To follow the steps demonstrated in this guide, you’ll need to have the following components ready to go.

  • The latest version of Ubuntu. Learn more about installing Ubuntu on VirtualBox. This eliminates the concern of installing and configuring a new operating system from scratch.
  • Familiarity with the command-line interface.

The Python version

Any Python version number has three components.

  • The major version
  • The minor version
  • The micro version

As mentioned before, Python 2 and Python 3 are the two major versions available. Python 2 is obsolete and the latest version released was Python 2.7.18. Here,

  • The major version: 2
  • The minor version: 7
  • The micro version: 18

At the time of writing this article, the latest Python 3 is Python 3.10.2. Here,

  • The major version: 3
  • The minor version: 10
  • The micro version: 2

Checking the Python version

Using the Python interpreter

Python comes the python command-line tool. It functions as both a Python shell and interpreter. We can ask this tool to print the version number of the Python installed.

To check the installed version of Python 2, run the following command:

$ python2 --version

To check the installed version of Python 3, run the following command:

$ python3 --version

Note that if you don’t specify the Python version, then it will use the default Python version. Note that you can have both Python 2 and Python 3 installed in the same system. Packages like python-is-python2 or python-is-python3 can influence the default Python version.

$ python --version

Using a Python script

We can create a simple Python script that will print the version info of the Python it’s running under.

The first example will incorporate the sys library. It comes with two ways of checking the version: the sys.version string and sys.version_info. In the following code, I’ve implemented both at the same time.

import sys
print(sys.version)
print(sys.version_info)

Another way is to use the platform library. Like sys, it also offers two ways of checking the Python version it’s running under the python_version() and python_version_tuple() functions. The following code demonstrates both of the functions at once.

import platform
print(platform.python_version())
print(platform.python_version_tuple())

Running Python codes without script

Instead of creating a dedicated script, we can run the Python codes directly from the command line. It compresses all the steps into a single command. For short and simple tasks like this, it’s a more optimal way.

Using the sys module

In the following command, we’re importing the sys module and printing the value of the sys.version.

$ python2 -c "import sys; print(sys.version)"

$ python3 -c "import sys; print(sys.version)"

Alternatively, you can print the value of sys.version_info.

$ python2 -c "import sys; print(sys.version_info)"

$ python3 -c "import sys; print(sys.version_info)"

Using the platform module

The following Python commands import the platform module and print the values of platform.python_version() and platform.python_version_tuple() functions.

$ python2 -c "import platform; print(platform.python_version())"

$ python3 -c "import platform; print(platform.python_version())"

$ python2 -c "import platform; print(platform.python_version_tuple())"

$ python3 -c "import platform; print(platform.python_version_tuple())"

Final Thoughts

In this short guide, we explored various ways to check the version of Python our code is running on. These techniques can come in handy when you’ve written a Python code that requires a specific version to function properly.

Interested in mastering Python? Check out our Python sub-category. It’s rich in Python tutorials on various Python concepts, suitable for beginners to advanced developers.

Happy Computing!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.