Python is one of the biggest open-source projects in programming history. It has one of the most active communities, with hundreds of new libraries being distributed and updated every year and the Python software getting updated occasionally. Different versions of the software may have different compatibility statuses. Some may be compatible with some other versions of the software, and some may not.
So building software that has a lot of dependencies will lead to compatibility issues. Due to this reason, we may need to check the version of Python in the program itself. It’s important to resolve the compatibility issues that may arise in software.
In this article, we’re going to see how we can check the Python version at Runtime to resolve any compatibility issues in our software. There are several libraries and modules in Python which helps us to detect Python version during runtime. We’re going to check each one of them.
Using the sys module
The sys module is an in-built module that provides many system-related functions and attributes. It provides methods to work with low-level stuff like os. It also provides ways to interact with environment variables.
The sys module also provides a method to detect the Python version during runtime. We will use it to detect the Python version during the runtime. It’s really simple and easy. Let’s see how we can do it.
Related: Learn the sys module in depth.
import sys print(sys.version)
sys.version gives you the current version of Python and also the date and when it was last updated. The version of any software is divided into 3 parts, with all three of them separated by periods. The first number is known as the major version. The current major version of Python is 3, as you can see in the above output. The second number is the minor version which is 9 right now. The third version is the micro version which is 13 now.
Now, the sys module also provides a method for just a specific part of the Python version. The
sys.version_info can give the major, minor, and micro versions separately. Let’s see how we can use it.
To print the major version, we used the function
sys.version_info.major. To print the minor version, we used the function
sys.version_info.minor. To print the micro version, we used the function
Using the platform module
The platform module is a system-related built-in module, just like the sys module. It can be used to work on data related to the platform where you’re programming. It has functions similar to sys and is really simple to use. The platform module also provides a method to detect the Python version during runtime. Let’s see how we can use the platform module to detect to Python version during runtime.
Related: Learn the platform module in depth.
import platform python_version = platform.python_version() print("Python version:", python_version)
platform.python_version returns a string as the Python version. So we can simply print the Python version by printing the
platform.python_version. Another method of the platform library returns the Python version as a tuple.
import platform version_tuple = platform.python_version_tuple() print(version_tuple) major_version = version_tuple minor_version = version_tuple micro_version = version_tuple print("Python version:", major_version+ "."+ minor_version+ "."+ micro_version)
platform.python_version_tuple the method returns a tuple instead of a string. The tuple consists of 3 elements. The first element of the string is the major Python version, the second is the minor Python version, and the last element is the micro Python version.
Using the command line arguments
You can also use the command line arguments to detect the Python version. Now, you can’t run command line arguments directly while running a program as your terminal is busy. But there are modules in Python that let you work with command-line arguments within the program. The
argparse module is one of them and is really convenient.
argparse module can be used to work with command-line arguments which in turn can be used to detect the version of Python. Let’s see how we can do it using the argparse module.
import argparse parser = argparse.ArgumentParser() parser.add_argument("--python-version") args = parser.parse_args() python_version = args.python_version print("Python version:", python_version)
First, we create an instance of the
ArgumentParser class. Then we add the
--python-version argument to the parser. Then we retrieve the value of the argument using the
Now while running the file in the terminal, you can specify the version within it and it will output the Python version.
There we go; we got the Python version. But the problem with this method is we have to pass the argument by ourselves every time we run the program.
Using the environment variables
Environment variables are the variables that store environment-related information like the
path and many more. There’s also an environment variable for the Python version. This environment variable can be read during runtime using the
sys module to detect the Python version. Let’s see how we can do it.
Related: What are the environment variables?
But first, you will set the environment variable python version, which you can read it later in the program. To set it, execute the following command in the terminal.
Now when you access the environment variable python_version in a program using the os module, you can retrieve the Python version that you’ve set.
import os python_version = os.environ.get("PYTHON_VERSION") print("Python version:", python_version)
This way, we can retrieve the Python version during runtime. But the problem here is the same. We’ll have to set the Python version in the shell in every new shell session.
Those were a few ways by which you can check the version of Python during runtime. Doing it is not that hard but quite important for building dynamic software. I hope this article helped you in your journey of software development. Make sure to check out more articles by our friends at askPython.
Stack Overflow thread for the same question.