Python sys.getwindowsversion() method



The Python sys.getwindowsversion() method that returns a named tuple containing detailed version information about the running Windows operating system. The named tuple includes five attributes such as major and minor version numbers, build number, platform identifier and service_pack level.

This method is useful for retrieving precise OS version details which can be critical for compatibility checks and debugging in Windows-specific Python applications.

Syntax

Following is the syntax and parameter of Python sys.getwindowsversion() method −
sys.getwindowsversion()

Parameter

This method does not take any parameters.

Return value

This method returns the named tuple.

Example 1

Following is the example of the python sys.getwindowsversion() method which prints the the named tuple with the Windows version information−

import sys

version_info = sys.getwindowsversion()
print(version_info)

Output

sys.getwindowsversion(major=10, minor=0, build=22000, platform=2, service_pack='')

Example 2

We know there are some specific attributes of the Windows version, so we can access and print them such as the major version, minor version, build number and service pack −

import sys

version_info = sys.getwindowsversion()
print(f"Major version: {version_info.major}")
print(f"Minor version: {version_info.minor}")
print(f"Build number: {version_info.build}")
print(f"Service pack: {version_info.service_pack}")

Output

Major version: 10
Minor version: 0
Build number: 22000
Service pack:

Example 3

This example checks if the current system is running Windows 10 by comparing the major and minor version numbers.−

import sys

version_info = sys.getwindowsversion()
if version_info.major == 10 and version_info.minor == 0:
    print("Running on Windows 10")
else:
    print("Not running on Windows 10")

Output

Running on Windows 10
python_modules.htm
Advertisements