Python: What OS am I running on?

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

    I started a bit more systematic listing of what values you can expect using the various modules (feel free to edit and add your system):

    Linux (64bit) + WSL

    os.name                     posix
    sys.platform                linux
    platform.system()           Linux
    sysconfig.get_platform()    linux-x86_64
    platform.machine()          x86_64
    platform.architecture()     ('64bit', '')
    
    • tried with archlinux and mint, got same results
    • on python2 sys.platform is suffixed by kernel version, e.g. linux2, everything else stays identical
    • same output on Windows Subsystem for Linux (tried with ubuntu 18.04 LTS), except platform.architecture() = ('64bit', 'ELF')

    WINDOWS (64bit)

    (with 32bit column running in the 32bit subsystem)

    official python installer   64bit                     32bit
    -------------------------   -----                     -----
    os.name                     nt                        nt
    sys.platform                win32                     win32
    platform.system()           Windows                   Windows
    sysconfig.get_platform()    win-amd64                 win32
    platform.machine()          AMD64                     AMD64
    platform.architecture()     ('64bit', 'WindowsPE')    ('64bit', 'WindowsPE')
    
    msys2                       64bit                     32bit
    -----                       -----                     -----
    os.name                     posix                     posix
    sys.platform                msys                      msys
    platform.system()           MSYS_NT-10.0              MSYS_NT-10.0-WOW
    sysconfig.get_platform()    msys-2.11.2-x86_64        msys-2.11.2-i686
    platform.machine()          x86_64                    i686
    platform.architecture()     ('64bit', 'WindowsPE')    ('32bit', 'WindowsPE')
    
    msys2                       mingw-w64-x86_64-python3  mingw-w64-i686-python3
    -----                       ------------------------  ----------------------
    os.name                     nt                        nt
    sys.platform                win32                     win32
    platform.system()           Windows                   Windows
    sysconfig.get_platform()    mingw                     mingw
    platform.machine()          AMD64                     AMD64
    platform.architecture()     ('64bit', 'WindowsPE')    ('32bit', 'WindowsPE')
    
    cygwin                      64bit                     32bit
    ------                      -----                     -----
    os.name                     posix                     posix
    sys.platform                cygwin                    cygwin
    platform.system()           CYGWIN_NT-10.0            CYGWIN_NT-10.0-WOW
    sysconfig.get_platform()    cygwin-3.0.1-x86_64       cygwin-3.0.1-i686
    platform.machine()          x86_64                    i686
    platform.architecture()     ('64bit', 'WindowsPE')    ('32bit', 'WindowsPE')
    
    

    Some remarks:

    • there is also distutils.util.get_platform() which is identical to `sysconfig.get_platform
    • anaconda on windows is same as official python windows installer
    • I don't have a Mac nor a true 32bit system and was not motivated to do it online

    To compare with your system, simply run this script (and please append results here if missing :)

    from __future__ import print_function
    import os
    import sys
    import platform
    import sysconfig
    
    print("os.name                      ",  os.name)
    print("sys.platform                 ",  sys.platform)
    print("platform.system()            ",  platform.system())
    print("sysconfig.get_platform()     ",  sysconfig.get_platform())
    print("platform.machine()           ",  platform.machine())
    print("platform.architecture()      ",  platform.architecture())
    

提交回复
热议问题