Python: What OS am I running on?

前端 未结 26 1566
野趣味
野趣味 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:03

    You can look at the code in pyOSinfo which is part of the pip-date package, to get the most relevant OS information, as seen from your Python distribution.

    One of the most common reasons people want to check their OS is for terminal compatibility and if certain system commands are available. Unfortunately, the success of this checking is somewhat dependent on your python installation and OS. For example, uname is not available on most Windows python packages. The above python program will show you the output of the most commonly used built-in functions, already provided by os, sys, platform, site.

    So the best way to get only the essential code is looking at that as an example. (I guess I could have just pasted it here, but that would not have been politically correct.)

    0 讨论(0)
  • 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: ('', ('', '', ''), '')
    
    0 讨论(0)
  • 2020-11-22 06:07

    I know this is an old question but I believe that my answer is one that might be helpful to some people who are looking for an easy, simple to understand pythonic way to detect OS in their code. Tested on python3.7

    from sys import platform
    
    
    class UnsupportedPlatform(Exception):
        pass
    
    
    if "linux" in platform:
        print("linux")
    elif "darwin" in platform:
        print("mac")
    elif "win" in platform:
        print("windows")
    else:
        raise UnsupportedPlatform
    
    0 讨论(0)
  • If you are running macOS X and run platform.system() you get darwin because macOS X is built on Apple's Darwin OS. Darwin is the kernel of macOS X and is essentially macOS X without the GUI.

    0 讨论(0)
  • 2020-11-22 06:08

    If you not looking for the kernel version etc, but looking for the linux distribution you may want to use the following

    in python2.6+

    >>> import platform
    >>> print platform.linux_distribution()
    ('CentOS Linux', '6.0', 'Final')
    >>> print platform.linux_distribution()[0]
    CentOS Linux
    >>> print platform.linux_distribution()[1]
    6.0
    

    in python2.4

    >>> import platform
    >>> print platform.dist()
    ('centos', '6.0', 'Final')
    >>> print platform.dist()[0]
    centos
    >>> print platform.dist()[1]
    6.0
    

    Obviously, this will work only if you are running this on linux. If you want to have more generic script across platforms, you can mix this with code samples given in other answers.

    0 讨论(0)
  • 2020-11-22 06:09

    Sample code to differentiate OS's using python:

    from sys import platform as _platform
    
    if _platform == "linux" or _platform == "linux2":
        # linux
    elif _platform == "darwin":
        # MAC OS X
    elif _platform == "win32":
        # Windows
    elif _platform == "win64":
        # Windows 64-bit
    
    0 讨论(0)
提交回复
热议问题