如何确定我的 python shell 是在 32 位还是 64 位中执行? [英] How do I determine if my python shell is executing in 32bit or 64bit?

查看:31
本文介绍了如何确定我的 python shell 是在 32 位还是 64 位中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来从 shell 内部判断 shell 处于什么模式.

I need a way to tell what mode the shell is in from within the shell.

虽然我主要是 OS X 用户,但我也有兴趣了解其他平台.

While I'm primarily an OS X user, I'd be interested in knowing about other platforms as well.

我试过查看 platform 模块,但它似乎只是告诉你关于关于位体系结构和用于可执行文件的链接格式":尽管二进制文件被编译为 64 位(我在 OS X 10.6 上运行),因此即使我正在使用这些方法,它似乎总是报告 64 位 此处描述 强制使用 32 位模式).

I've tried looking at the platform module but it seems only to tell you about "about the bit architecture and the linkage format used for the executable": the binary is compiled as 64bit though (I'm running on OS X 10.6) so it seems to always report 64bit even though I'm using the methods described here to force 32bit mode).

推荐答案

一种方法是查看 sys.maxsize 记录的 这里:

One way is to look at sys.maxsize as documented here:

$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffff', False)
$ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)

sys.maxsize 是在 Python 2.6 中引入的.如果您需要对旧系统进行测试,这个稍微复杂的测试应该适用于所有 Python 2 和 3 版本:

sys.maxsize was introduced in Python 2.6. If you need a test for older systems, this slightly more complicated test should work on all Python 2 and 3 releases:

$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64

顺便说一句,您可能会为此使用 platform.architecture() .不幸的是,它的结果并不总是可靠的,尤其是在 OS X 通用二进制文件的情况下.

BTW, you might be tempted to use platform.architecture() for this. Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries.

$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False

这篇关于如何确定我的 python shell 是在 32 位还是 64 位中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆