What Version of Python Is This?

Just like finding out what version of Windows someone is running can be tough, figuring out what version of Python itself can be harder. Finding out what version of Python is being used is arguably more important than finding the Windows version.

Right now, there are two major versions of Python in use: Python 2, and Python 3. In the minor version, it’s Python 2.7 and Python 3.3 (although some devs use Python 3.2). Python 2.x is the de-facto standard, while Python 3.x is the current and future version of the language. There are many, many changes between Python 2 and 3, so much that a good number of external modules were broken with the release of 3.0 (it wasn’t called “first ever intentionally backwards incompatible Python release” for just any old reason).

Because of these changes between the two versions, it is usually necessary to detect what version of Python is being used to run your program, and either stop it (if there is incompatible code) or adapt it (if it is Python 2 and 3 compatible). However, detecting the version is not very straight forward.

In this tutorial, I will outline a few methods for getting the Python version. What do to with that info is completely up to you. 😛

While all code should be Python 2 and 3 compatible unless otherwise noted, any Exceptions are from Python 3.3+.

Method #1: The sys Module

When the code above is run, it looks like this:

Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
You are running 3.3.1
You need at least Python 3.3.0 to run this.
You need at least Python 2.5 to run this.
You are running Python 3.3.1 You need to get Python 2.7.3 to run this
>>>

Method #2: The platform Module

And when the code is run, it looks like this:

Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
3.3.1
('3', '3', '1')
You are running Python 3.3.1 . You need to get Python 2.7.3 to run this
>>>

As you can see, the platform module is a bit easier to use, but you still have to import sys to have the compatible “print” function. In the end, it is really up to you, the developer, to choose what method to use.

That’s it! You now know how to find out how to get the Python version being used, and do whatever you want with that info! 😀

One thought on “What Version of Python Is This?

Triangular Reactions

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s