Reliably detect Windows in Python

前端 未结 6 1386
半阙折子戏
半阙折子戏 2020-12-12 21:46

I\'m working on a couple of Linux tools and need to prevent installation on Windows, since it depends on FHS and is thus rendered useless on that platform. The platfor

6条回答
  •  鱼传尺愫
    2020-12-12 22:31

    For those that came here looking for a way to detect Cygwin from Python (as opposed to just detecting Windows), here are some example return values from os.name and platform.system on different platforms

    OS/build     | os.name | platform.system() 
    -------------+---------+-----------------------
    Win32 native | nt      | Windows
    Win32 cygwin | posix   | CYGWIN_NT-5.1*
    Win64 native | nt      | Windows
    Win64 cygwin | posix   | CYGWIN_NT-6.1-WOW64*
    Linux        | posix   | Linux
    

    From this point, how to distinguish between Windows native and Cygwin should be obvious although I'm not convinced this is future proof.

    * version numbers are for XP and Win7 respectively, do not rely on them

提交回复
热议问题