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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2144748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:49:34  来源:igfitidea点击:

Is it safe to use sys.platform=='win32' check on 64-bit Python?

pythonwindows64-bitcross-platform32bit-64bit

提问by bialix

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

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

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?

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

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

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

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

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

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

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.

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

采纳答案by Torsten Marek

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

sys.platform将不win32考虑底层 Windows 系统的位数,正如您在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"

It's possible to find the original patchthat 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.

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

回答by Sridhar Ratnakumar

Personally I use platinfofor detecting the underlying platform.

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

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

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

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

回答by Pekka Kl?rck

Notice that you cannot use either sys.platformor os.namefor this on Jython:

请注意,您不能在 Jython 上使用任何一个sys.platformos.name为此:

$ 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.nameto 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._nameon Jython 2.5.x:

我认为 Jython 项目中有一个计划来改变os.name报告底层操作系统与 CPython 类似,但因为人们正在使用os.name == 'java'检查他们是否在 Jython 上,这种改变不能在一夜之间完成。但是,os._nameJython 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.

我个人倾向于使用os.sep == '/'需要在 Jython 和 CPython 以及 Windows 和 unixy 平台上运行的代码。这有点难看,但有效。

回答by Ignacio Vazquez-Abrams

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.maxintand ctypes. If you need to distinguish between 32 and 64 regardless then platformis your best bet.

Windows/32 和 Windows/64 的警告是相同的,所以它们应该使用相同的值。唯一的区别在于 egsys.maxintctypes。如果您无论如何都需要区分 32 和 64,那么这platform是您最好的选择。