什么是 linux 相当于 sys.getwindowsversion()

What is the linux equivalent of sys.getwindowsversion()

没有sys.getlinuxversion(),我觉得我可能必须从几个地方拼凑起来(这很好);但是,也许不是?

import sys, os
from collections import namedtuple

## This defines an interface similar to the object that getwindowsversion() returns
## (I say 'similar' so I don't have to explain the descriptor magic behind it)
WindowsVersion = namedtuple('WindowsVersion', 'major minor build platform product_type service_pack service_pack_major service_pack_minor suite_mask')

## Looking for something that quacks like this:
LinuxVersion = namedtuple('LinuxVersion', 'major minor build whatever attrs are needed')

## To create something that quacks similarly to this (in case you're wondering)
PlatformVersion = namedtuple('PlatformVersion', 'major minor micro build info')

## EDIT: A newer version of Version, perhaps?
class Version:
    _fields = None
    def __init__(self, *subversions, **info)
        self.version = '.'.join([str(sub) for sub in subversions])
        self.info = info
        instance_fields = self.__class__._fields
        if instance_fields is not None:
            if isinstance(instance_fields, str):
                instance_fields = instance_fields.split()
            for field in instance_fields:
                if field in self.info:
                    setattr(self, field, self.info.pop(field))

## This makes the PlatformVersion (or any other Version) definition a mere:
class PlatformVersion(Version):
    _fields = 'build'  ## :)

## Now PlatformInfo looks something like:
class PlatformInfo:
    def __init__(self, *version_number, platform=None, version_info=None, **info):
        self.platform = sys.platform if platform is None else platform
        self.info = info
        if self.platform in ('win32', 'win64'):
            works_great = sys.getwindowsversion()
            self.version = PlatformVersion(works_great.major, works_great.minor, works_great.service_pack_major, build=works_great.build, **dict(version_info))
        else:
            self.version = your_answer(PlatformVersion(os.uname().release, build=os.uname().version, **dict(version_info))), 'hopefully'
            self.version = self.version[0]  ## lol

谢谢!

Linux(以及所有类 Unix 系统)有 uname 提供此类信息的系统调用:

>>> import os
>>> os.uname().release
'3.11.10-17-desktop'

显示内核版本。

请注意,在 Python 2 上,它将 return 元组而不是命名元组:

>>> import os
>>> os.uname()[2]
'3.11.10-17-desktop'