使用 sys.platform=='win32' 是否安全?检查 64 位 Python?

发布于 2024-08-18 22:27:07 字数 566 浏览 3 评论 0原文

区分在 Windows 和其他操作系统(通常是 Linux)上运行 Python 应用程序的通常检查是使用条件:

if sys.platform == 'win32':
    ...

但我想知道在过去几年 64 位 Python 得到更广泛使用的情况下,今天使用它是否安全? 32 真的意味着 32 位吗,还是基本上指的是 Win32 API?

如果有可能有一天 sys.platform 为“win64”,也许这种情况会更普遍?

if sys.platform.startswith('win'):
    ...

我知道还有另一种检测 Windows 的方法:

if os.name == 'nt':
    ...

但我真的从未在其他代码中看到后者的使用。

那什么是最好的方法呢?

UPD:如果可以的话,我想避免使用额外的库。对于 Linux 用户来说,需要安装额外的库来检查我是否在 Windows 中工作可能会很烦人。

The usual check to differentiate between running Python-application on Windows and on other OSes (Linux typically) is to use conditional:

if sys.platform == 'win32':
    ...

But I wonder is it safe to use today when 64-bit Python is more widely used in last years? Does 32 really means 32-bit, or basically it refers to Win32 API?

If there is possibility to have one day sys.platform as 'win64' maybe such condition would be more universal?

if sys.platform.startswith('win'):
    ...

There is also another way to detect Windows I'm aware of:

if os.name == 'nt':
    ...

But I really never saw in other code the use of the latter.

What is the best way then?

UPD: I'd like to avoid using extra libraries if I can. Requiring installing extra library to check that I'm work not in the Windows may be annoying for Linux users.

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

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

发布评论

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

评论(4

心如狂蝶 2024-08-25 22:27:07

无论底层 Windows 系统的位数如何,sys.platform 都将是 win32,正如您在 PC/pyconfig.h 中看到的那样(来自Python 2.6 源代码分发):

#if defined(MS_WIN64)
/* maintain "win32" sys.platform for backward compatibility of Python code,
   the Win64 API should be close enough to the Win32 API to make this
   preferable */
#       define PLATFORM "win32"

可以找到原始补丁网上对此进行了介绍,其中提供了更多解释:

主要问题是:Win64 是否更像 Win32,而不是不同之处,以至于普通 Python 程序员不必在他的 Python 代码中进行区分。或者,至少,足以使得 Python 脚本编写者的这种区分非常罕见,以至于其他提供的机制就足够了(甚至更可取)。目前答案是肯定的。希望微软不会改变这个答案。

sys.platform will be win32 regardless of the bitness of the underlying Windows system, as you can see in PC/pyconfig.h (from the Python 2.6 source distribution):

#if defined(MS_WIN64)
/* maintain "win32" sys.platform for backward compatibility of Python code,
   the Win64 API should be close enough to the Win32 API to make this
   preferable */
#       define PLATFORM "win32"

It's possible to find the original patch that introduced this on the web, which offers a bit more explanation:

The main question is: is Win64 so much more like Win32 than different from it that the common-case general Python programmer should not ever have to make the differentiation in his Python code. Or, at least, enough so that such differentiation by the Python scriptor is rare enough that some other provided mechanism is sufficient (even preferable). Currently the answer is yes. Hopefully MS will not change this answer.

别挽留 2024-08-25 22:27:07

我个人使用 platinfo 来检测底层平台。

>>> from platinfo import PlatInfo
>>> pi = PlatInfo()
>>> pi.os
'win64'
>>> pi.arch
'x64'
>>> pi.name()
'win64-x64'

对于 32 位,pi.name() 返回 win32-x86

Personally I use platinfo for detecting the underlying platform.

>>> from platinfo import PlatInfo
>>> pi = PlatInfo()
>>> pi.os
'win64'
>>> pi.arch
'x64'
>>> pi.name()
'win64-x64'

For 32-bit, pi.name() returns win32-x86.

月下凄凉 2024-08-25 22:27:07

请注意,您不能在 Jython 上使用 sys.platformos.name 来实现此目的:

$ jython -c "import sys, os; print sys.platform; print os.name"
java1.6.0_20
java

我认为 Jython 项目中有一个计划来更改 os.name 来报告底层操作系统,与 CPython 类似,但由于人们使用 os.name == 'java' 来检查他们是否使用 Jython,因此这种更改不可能一蹴而就。然而,Jython 2.5.x 上已经有了 os._name:就

$ jython -c "import os; print os._name"
posix

我个人而言,我倾向于将 os.sep == '/' 与需要同时运行的代码一起使用Jython 和 CPython,并且都在 Windows 和 unixy 平台上。它有点丑陋但有效。

Notice that you cannot use either sys.platform or os.name for this on Jython:

$ jython -c "import sys, os; print sys.platform; print os.name"
java1.6.0_20
java

I think there's a plan in Jython project to change os.name to report the underlying OS similarly as CPython, but because people are using os.name == 'java' to check are they on Jython this change cannot be done overnight. There is, however, already os._name on Jython 2.5.x:

$ jython -c "import os; print os._name"
posix

Personally I tend to use os.sep == '/' with code that needs to run both on Jython and CPython, and both on Windows and unixy platforms. It's somewhat ugly but works.

一百个冬季 2024-08-25 22:27:07

Windows/32 和 Windows/64 的注意事项是相同的,因此它们应该使用相同的值。唯一的区别在于例如sys.maxintctypes。如果您需要区分 32 和 64,那么 platform 是您的最佳选择。

The caveats for Windows/32 and Windows/64 are the same, so they should use the same value. The only difference would be in e.g. sys.maxint and ctypes. If you need to distinguish between 32 and 64 regardless then platform is your best bet.

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