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

前端 未结 18 1142
北恋
北恋 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:46

    On my Centos Linux system I did the following:

    1) Started the Python interpreter (I'm using 2.6.6)
    2) Ran the following code:

    import platform
    print(platform.architecture())
    

    and it gave me

    (64bit, 'ELF')
    
    0 讨论(0)
  • 2020-11-22 04:49

    On Windows OS

    Open the cmd termial and start python interpreter by typing >python as shown in the below image

    If the interpreter info at start contains AMD64, it's 64-bit, otherwise, 32-bit bit.

    0 讨论(0)
  • 2020-11-22 04:49

    Platform Architecture is not the reliable way. Instead us:

    $ arch -i386 /usr/local/bin/python2.7
    Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import platform, sys
    >>> platform.architecture(), sys.maxsize
    (('64bit', ''), 2147483647)
    >>> ^D
    $ arch -x86_64 /usr/local/bin/python2.7
    Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import platform, sys
    >>> platform.architecture(), sys.maxsize
    (('64bit', ''), 9223372036854775807)
    
    0 讨论(0)
  • 2020-11-22 04:53
    C:\Users\xyz>python
    
    Python 2.7.6 (default, Nov XY ..., 19:24:24) **[MSC v.1500 64 bit (AMD64)] on win
    32**
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    after hitting python in cmd

    0 讨论(0)
  • 2020-11-22 04:55

    For 32 bit it will return 32 and for 64 bit it will return 64

    import struct
    print(struct.calcsize("P") * 8)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题