How do I determine if my python shell is executing in 32bit or 64bit?

前端 未结 18 1074
北恋
北恋 2020-11-22 04:25

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

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

18条回答
  •  囚心锁ツ
    2020-11-22 04:57

    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 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
    

    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
    

提交回复
热议问题