How to detect Mac OS version using Python?

后端 未结 5 615
醉梦人生
醉梦人生 2020-12-28 15:56

My application is assumed to be running on a Mac OS X system. However, what I need to do is figure out what version of Mac OS (or Darwin) it is running on, preferably as a

相关标签:
5条回答
  • 2020-12-28 16:26

    If you are already using os, you might want to use os.uname()

    import os
    os.uname()
    
    0 讨论(0)
  • 2020-12-28 16:27

    You could parse the output of the /usr/bin/sw_vers command.

    0 讨论(0)
  • 2020-12-28 16:30
    >>> import platform
    >>> platform.mac_ver()
    ('10.5.8', ('', '', ''), 'i386')
    

    As you see, the first item of the tuple mac_ver returns is a string, not a number (hard to make '10.5.8' into a number!-), but it's pretty easy to manipulate the 10.x.y string into the kind of numbers you want. For example,

    >>> v, _, _ = platform.mac_ver()
    >>> v = float('.'.join(v.split('.')[:2]))
    >>> print v
    10.5
    

    If you prefer the Darwin kernel version rather than the MacOSX version, that's also easy to access -- use the similarly-formatted string that's the third item of the tuple returned by platform.uname().

    0 讨论(0)
  • 2020-12-28 16:33

    If you want to run a command - like 'uname' - and get the results as a string, use the subprocess module.

    import subprocess
    output = subprocess.Popen(["uname", "-r"], stdout=subprocess.PIPE).communicate()[0]
    
    0 讨论(0)
  • 2020-12-28 16:34

    platform.mac_ver() will return a tuple (release, versioninfo, machine

    So get mac version by code

    >>> platform.mac_ver()[0]
    
    '10.8.4'
    

    this method is easy

    0 讨论(0)
提交回复
热议问题