简体   繁体   English

在 Python 中可靠地检测 Windows

[英]Reliably detect Windows in Python

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.我正在研究几个 Linux 工具,需要防止在 Windows 上安装,因为它依赖于 FHS,因此在该平台上变得无用。 The platform.platform function comes close but only returns a string. platform.platform函数接近但只返回一个字符串。

Unfortunately I don't know what to search for in that string for it to yield a reliable result.不幸的是,我不知道要在该字符串中搜索什么才能产生可靠的结果。 Does anyone know what to search for or does anyone know of another function that I'm missing here?有谁知道要搜索什么,或者有人知道我在这里缺少的另一个功能吗?

>>> import platform
>>> platform.system()
'Windows'

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对于那些来到这里寻找从 Python 检测 Cygwin 的方法(而不是仅仅检测 Windows)的人,这里有一些来自不同平台上的os.nameplatform.system的示例返回值

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.从这一点来看,如何区分 Windows 本机和 Cygwin 应该是显而易见的,尽管我不相信这是未来的证明。

* version numbers are for XP and Win7 respectively, do not rely on them * 版本号分别针对XP和Win7,不要依赖它们

On my Windows box, platform.system() returns 'Windows' .在我的 Windows 机器上, platform.system()返回'Windows'

However, I'm not sure why you'd bother.但是,我不确定您为什么要打扰。 If you want to limit the platform it runs on technologically, I'd use a white-list rather than a black-list.如果你想在技术上限制它运行的平台,我会使用白名单而不是黑名单。

In fact, I wouldn't do it technologically at all since perhaps the next release of Python may have Win32/Win64 instead of Windows (for black-listing) and *nix instead of Linux (for white-listing).事实上,我根本不会在技术上这样做,因为也许下一个版本的 Python 可能有Win32/Win64而不是Windows (用于黑名单)和*nix而不是Linux (用于白名单)。

My advice is to simply state what the requirements are and, if the user chooses to ignore that, that's their problem.我的建议是简单地说明需求是什么,如果用户选择忽略它,那就是他们的问题了。 If they ring up saying they got an error message stating "Cannot find FHS" and they admit they're running on Windows, gently point out to them that it's not a supported configuration.如果他们打电话说他们收到一条错误消息,指出“无法找到 FHS”并且他们承认他们正在 Windows 上运行,请轻轻地向他们指出它不是受支持的配置。

Maybe your customers are smart enough to get FHS running under Windows so that your code will work.也许您的客户足够聪明,可以让 FHS 在 Windows 下运行,这样您的代码才能正常工作。 They're unlikely to appreciate what they would then consider an arbitrary limitation of your software.他们不太可能欣赏他们随后会考虑对您的软件进行任意限制的内容。

This is a problem faced by software developers every day.这是软件开发人员每天都要面对的问题。 Even huge organizations can't support every single platform and configuration out there.即使是大型组织也无法支持那里的一个平台和配置。

>>> import os
>>> os.name
'nt'

"The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'mac', 'os2', 'ce', 'java', 'riscos'." "导入的操作系统依赖模块的名称。目前已经注册了以下名称:'posix'、'nt'、'mac'、'os2'、'ce'、'java'、'riscos'。" (c) http://docs.python.org/library/os.html#os.name (c) http://docs.python.org/library/os.html#os.name

import os
if os.name == 'nt':
    #yourcodehere

Try this:试试这个:

import platform

if platform.system() == "Darwin":
    # Don't have Windows handy, but I'd expect "Win32" or "Windows" for it

Edit: Just saw that you tried platform.platform() ... platform.system() will work better for this case.编辑:刚刚看到您尝试过platform.platform() ... platform.system()对于这种情况会更好。 Trust me, use it.相信我,使用它。 Dark corners lie in platform detection.暗角在于平台检测。

distutils will do this too, if you ask it nicely. distutils也会这样做,如果你问得好的话。

You could always do something bad like os.path.exists() on a Windows file...but platform is as reliable as it gets in the Python standard library.你总是可以在 Windows 文件上做一些坏事,比如os.path.exists() ……但是platform和 Python 标准库一样可靠。

Edit 2: Another helpful answerer pointed out platform.system() is exactly equal to "Windows" on his Windows machine.编辑 2:另一个有帮助的回答者指出platform.system()完全等于他的 Windows 机器上的“Windows”。

From help(platform)来自help(platform)

system()
    Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.

    An empty string is returned if the value cannot be determined.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM