Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Windows 8.1 in Python?

Tags:

python

windows

We've got a script that uses the platform module to detect OS version of our various clients.

Looking through the source for platform.py, I can see that on Windows systems, it's using sys.getwindowsverion(). Unfortunately, on Windows 8.1 systems, that particular function reports:

>>> sys.getwindowsversion()
sys.getwindowsversion(major=6, minor=2, build=9200, platform=2, service_pack='')

Windows 8.1 is 6.3.9600:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Windows\system32>ver

Microsoft Windows [Version 6.3.9600]

So, I realize I could write some extra logic around my call to platform.release(), and if that returns 8, do a secondary check and try to run ver, but that seems slightly convoluted.

Does anyone know of a better way?

Running ActivePython 2.7.2.5 in case that matters . . .

like image 597
ernie Avatar asked Oct 02 '13 00:10

ernie


People also ask

Does Windows 8.1 support Python?

This means that Python 3.10 supports Windows 8.1 and newer. If you require Windows 7 support, please install Python 3.8.

What version of Python do I have Windows 8?

Open the cmd and type py --version. The cmd will show you the Python version of Python installed.

How do you check Python is installed or not in Windows 8?

To check whether PIP is installed on your windows system just type “pip -V” in your windows command prompt.


2 Answers

Microsoft changed the way that the version function behave. See this page for some more information.

The way that I worked around the problem is to use ctypes and a kernel mode function, RtlGetVersion. Despite this being a kernel mode function it can be called from user mode just fine. I've tried this on many versions of Windows and not had an issue.

import ctypes

class OSVERSIONINFOEXW(ctypes.Structure):
    _fields_ = [('dwOSVersionInfoSize', ctypes.c_ulong),
                ('dwMajorVersion', ctypes.c_ulong),
                ('dwMinorVersion', ctypes.c_ulong),
                ('dwBuildNumber', ctypes.c_ulong),
                ('dwPlatformId', ctypes.c_ulong),
                ('szCSDVersion', ctypes.c_wchar*128),
                ('wServicePackMajor', ctypes.c_ushort),
                ('wServicePackMinor', ctypes.c_ushort),
                ('wSuiteMask', ctypes.c_ushort),
                ('wProductType', ctypes.c_byte),
                ('wReserved', ctypes.c_byte)]

def get_os_version():
    """
    Get's the OS major and minor versions.  Returns a tuple of
    (OS_MAJOR, OS_MINOR).
    """
    os_version = OSVERSIONINFOEXW()
    os_version.dwOSVersionInfoSize = ctypes.sizeof(os_version)
    retcode = ctypes.windll.Ntdll.RtlGetVersion(ctypes.byref(os_version))
    if retcode != 0:
        raise Exception("Failed to get OS version")

    return os_version.dwMajorVersion, os_version.dwMinorVersion
like image 116
Isaac Avatar answered Oct 19 '22 01:10

Isaac


You can simple get it from registry HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion there you have value name CurrentVersion And the Data in Windows 8.1 will be 6.3 It will work an any windows platform

like image 42
Uri Nahshon Avatar answered Oct 19 '22 01:10

Uri Nahshon