detect Windows version in python

Recently I wrote an application which needs Visual C++ 9.0 Runtime (Or Visual C++ 2008 Redistributable) installed. To make this application more friendly to users, detecting Visual C++ Runtime is added to beginning of program. If Visual C++ runtime is not installed on user’s operating system, program will download and install it automatically.

But another problem came, Visual C++ 9.0 Runtime is shipped with Windows 7, we cannot detect whether it’s installed just by scanning registry keys. One solution is to detect Windows version first, if Windows version is equal or later than Windows 7, then my application need not concern C++ Runtime things anymore

 

In general there are two ways to get windows version in python, one is using getwindowsversion() method of sys module, the other is through platform() method of platform module.

sys.getwindowsversion()

sys.getwindowsversion()

On my windows 7 machine, Running above code in interactive python interpreter will give me following results:

sys.getwindowsversion(major=6, minor=1, build=7600, platform=2, service_pack=”)

First let’s look at platform value, platform=2 means that currently running system is Windows NT platform.

Combining major value and minor value can determine a specific version of system. e.g. Windows NT 6.0 (major value 6 and minor value 0) means Windows Vista or Windows 2008, Windows NT 6.1 shown above tells us it’s Windows 7 or Windows 2008 R2.

You can view this page for a complete reference of Windows operating systems and their responding major version and minor version.

platform module

platform.platform()

will give us an descriptive string:

‘Windows-7-6.1.7600’

Let me explain it in details: Windows-7 is the operating system name well known between users, following 6.1.7600 is meaning major version is 6, minor version is 1 and build No. is 7600

Compared with sys.getwindowsversion(), platform() method is more friendly:)

 

  • Dave Lindbergh

    sys.getwindowsversion() returns the same values for Windows 7+ (Win7, Win8, and Win10). So – not useful to distinguish between them.