Python: What OS am I running on?

前端 未结 26 2039
野趣味
野趣味 2020-11-22 05:44

What do I need to look at to see whether I\'m on Windows or Unix, etc?

26条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 06:06

    If you want user readable data but still detailed, you can use platform.platform()

    >>> import platform
    >>> platform.platform()
    'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'
    

    Here's a few different possible calls you can make to identify where you are

    import platform
    import sys
    
    def linux_distribution():
      try:
        return platform.linux_distribution()
      except:
        return "N/A"
    
    print("""Python version: %s
    dist: %s
    linux_distribution: %s
    system: %s
    machine: %s
    platform: %s
    uname: %s
    version: %s
    mac_ver: %s
    """ % (
    sys.version.split('\n'),
    str(platform.dist()),
    linux_distribution(),
    platform.system(),
    platform.machine(),
    platform.platform(),
    platform.uname(),
    platform.version(),
    platform.mac_ver(),
    ))
    

    The outputs of this script ran on a few different systems (Linux, Windows, Solaris, MacOS) and architectures (x86, x64, Itanium, power pc, sparc) is available here: https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version

    Ubuntu 12.04 server for example gives:

    Python version: ['2.6.5 (r265:79063, Oct  1 2012, 22:04:36) ', '[GCC 4.4.3]']
    dist: ('Ubuntu', '10.04', 'lucid')
    linux_distribution: ('Ubuntu', '10.04', 'lucid')
    system: Linux
    machine: x86_64
    platform: Linux-2.6.32-32-server-x86_64-with-Ubuntu-10.04-lucid
    uname: ('Linux', 'xxx', '2.6.32-32-server', '#62-Ubuntu SMP Wed Apr 20 22:07:43 UTC 2011', 'x86_64', '')
    version: #62-Ubuntu SMP Wed Apr 20 22:07:43 UTC 2011
    mac_ver: ('', ('', '', ''), '')
    

提交回复
热议问题