使用 sys.platform=='win32' 是否安全?检查 64 位 Python?
区分在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无论底层 Windows 系统的位数如何,
sys.platform
都将是win32
,正如您在PC/pyconfig.h
中看到的那样(来自Python 2.6 源代码分发):可以找到原始补丁网上对此进行了介绍,其中提供了更多解释:
sys.platform
will bewin32
regardless of the bitness of the underlying Windows system, as you can see inPC/pyconfig.h
(from the Python 2.6 source distribution):It's possible to find the original patch that introduced this on the web, which offers a bit more explanation:
我个人使用 platinfo 来检测底层平台。
对于 32 位,
pi.name()
返回win32-x86
。Personally I use platinfo for detecting the underlying platform.
For 32-bit,
pi.name()
returnswin32-x86
.请注意,您不能在 Jython 上使用
sys.platform
或os.name
来实现此目的:我认为 Jython 项目中有一个计划来更改
os.name 来报告底层操作系统,与 CPython 类似,但由于人们使用 os.name == 'java'
来检查他们是否使用 Jython,因此这种更改不可能一蹴而就。然而,Jython 2.5.x 上已经有了os._name
:就我个人而言,我倾向于将
os.sep == '/'
与需要同时运行的代码一起使用Jython 和 CPython,并且都在 Windows 和 unixy 平台上。它有点丑陋但有效。Notice that you cannot use either
sys.platform
oros.name
for this on Jython: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 usingos.name == 'java'
to check are they on Jython this change cannot be done overnight. There is, however, alreadyos._name
on Jython 2.5.x: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.Windows/32 和 Windows/64 的注意事项是相同的,因此它们应该使用相同的值。唯一的区别在于例如
sys.maxint
和ctypes
。如果您需要区分 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
andctypes
. If you need to distinguish between 32 and 64 regardless thenplatform
is your best bet.