在 Python 中可靠地检测 Windows

发布于 2024-08-04 05:01:54 字数 183 浏览 6 评论 0原文

我正在开发几个 Linux 工具,需要阻止在 Windows 上安装,因为它依赖于 FHS,因此在该平台上毫无用处。 platform.platform 函数很接近,但只返回一个字符串。

不幸的是,我不知道在该字符串中搜索什么才能产生可​​靠的结果。有谁知道要搜索什么,或者有人知道我在这里缺少的另一个功能吗?

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 platform.platform function comes close but only returns a string.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

入怼 2024-08-11 05:01:54
>>> import platform
>>> platform.system()
'Windows'
>>> import platform
>>> platform.system()
'Windows'
紫竹語嫣☆ 2024-08-11 05:01:54

对于那些来这里寻找从 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

从这一点来看,如何区分 Windows 本机和 Cygwin 应该是显而易见的,尽管我不相信这是面向未来的。

* 版本号分别针对XP和Win7,请勿依赖它们

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

情独悲 2024-08-11 05:01:54

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

不过,我不确定你为什么要这么麻烦。如果您想从技术上限制它运行的平台,我会使用白名单而不是黑名单。

事实上,我根本不会在技术上这样做,因为也许 Python 的下一个版本可能有 Win32/Win64 而不是 Windows (用于黑名单)和 *nix 而不是 Linux(用于白名单)。

我的建议是简单地说明需求是什么,如果用户选择忽略这一点,那就是他们的问题。如果他们打电话说收到一条错误消息,指出“找不到 FHS”,并且他们承认自己在 Windows 上运行,请温和地向他们指出这不是受支持的配置。

也许您的客户足够聪明,可以让 FHS 在 Windows 下运行,以便您的代码可以运行。他们不太可能理解他们会认为你的软件的任意限制。

这是软件开发人员每天面临的问题。即使是大型组织也无法支持每一个平台和配置。

On my Windows box, platform.system() returns '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).

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.

Maybe your customers are smart enough to get FHS running under Windows so that your code will work. 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.

说不完的你爱 2024-08-11 05:01:54
>>> import os
>>> os.name
'nt'

“导入的操作系统相关模块的名称。当前已注册以下名称:'posix'、'nt'、'mac'、'os2'、'ce'、'java'、'riscos'。” (c) http://docs.python.org/library/os.html#操作系统名称

import os
if os.name == 'nt':
    #yourcodehere
>>> 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'." (c) http://docs.python.org/library/os.html#os.name

import os
if os.name == 'nt':
    #yourcodehere
野鹿林 2024-08-11 05:01:54

试试这个:

import platform

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

编辑:刚刚看到您尝试了platform.platform()...platform.system()对于这种情况会更好。相信我,使用它。平台检测存在暗角。

如果你问得好的话,distutils 也会这样做。

您总是可能在 Windows 文件上做一些不好的事情,例如 os.path.exists() 等……但是 platform 与 Python 标准库中的一样可靠。

编辑2:另一个有用的回答者指出platform.system()在他的Windows机器上完全等于“Windows”。

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. Trust me, use it. Dark corners lie in platform detection.

distutils will do this too, if you ask it nicely.

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.

Edit 2: Another helpful answerer pointed out platform.system() is exactly equal to "Windows" on his Windows machine.

疑心病 2024-08-11 05:01:54

来自帮助(平台)

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

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

From 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文