How do I determine the Python Shell is performed in 32-bit or 64-bit mode on OS X?

I need a way to tell from the shell in the shell in which mode.

I try to view platform module, but it seems only tell you that information "about the executable bit architecture used and the link format": Although binary files compiled for 64-bit (I'm running on OS X 10.6), so even if I use this describes the methods used to force the 32-bit mode, it always seemed to report 64.


#1st Floor

In the terminal / command line when you start the Python interpreter, you may also see a line similar to the following:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32

Wherein [MSC v.1500 64 bit (AMD64)]represents 64 Python. Apply to my specific settings.


#2nd Floor

Update: One way is to look at sys.maxsizeas the record 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.maxsizeIntroduced in Python 2.6. If you need to test for than the old system, this is slightly more complicated test should apply to all versions of Python 2 and 3:

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

By the way, you might want to use for this purpose platform.architecture(). Unfortunately, the results are not always reliable, particularly where the 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

#3rd floor

Try to use void pointers to get ctypes size:

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

4 to 32 will be, it will be 8 to 64.


#4th floor

Substantially Matthew Marshall (Matthew Marshall) to answer a variant (with a structure derived from a standard library):

import struct
print struct.calcsize("P") * 8

#5th Floor

For non-programming solutions, please view the Activity Monitor. 64-bit architecture will process as "Intel (64 bit)."


#6th floor

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

Click cmd in the python


#7th floor

Centos Linux on my system, I have done the following:

1) Start the Python interpreter (I'm using 2.6.6)
2) Run the following code:

import platform
print(platform.architecture())

It gave me

(64bit, 'ELF')

Building # 8

platform.architecture()Comment said:

Note: On Mac OS X (and possibly other platforms), the executable file may contain a variety of common file system structure.

To get the current interpreter of "64," the query sys.maxsize property more reliable:

import sys
is_64bits = sys.maxsize > 2**32

House # 9

Open the python console:

import platform
platform.architecture()[0]

It should be based on your platform to display "64bit" or "32bit".

Or ( for OS X binary files ):

import sys
sys.maxsize > 2**32 
# it should display True in case of 64bit and False in case of 32bit

#10th floor

struct.calcsize("P")Returns a pointer necessary to store a single byte size. In the system 32, it will return four bytes. In the system 64, it returns eight bytes.

So the following returns 32, 32 Python and if you're running 6464-bit python if you are running:

Python 2

import struct;print struct.calcsize("P") * 8

Python 3

import struct;print(struct.calcsize("P") * 8)

House # 11

import sys
print(sys.version)

3.5.1 (v3.5.1: 37a07cee5969, 2015 Nian 12 Yue 6 Ri, 01: 54: 25) [the MSC v.1900 64-Wei (AMD64) ]


House # 12

It is not a reliable method platform architecture. Instead, we:

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

House # 13

Grouping all ...

considering:

  • Asked OSX problem (I have an old (crack) VM, with an old Python version)
  • My main environment Win
  • I Win on only installed 32-bit version (and I Lnx build a "crippled" on the version )

I will use Python 3 and Python 2 were examples across all three platforms.

  1. Check the [Python 3.Docs]: sys. maxsize value - it with 0x100000000( 2 ** 32comparative): For 64 , the larger, for the 32 bit smaller:
    • OSX 9 x64
      • Python 2.7.10 x64
         >>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 2.7.10 (default, Oct 14 2015, 05:51:29) \\n[GCC 4.8.2] on darwin' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True) 
    • Ubuntu 16 x64
      • Python 3.5.2 x64
         >>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.5.2 (default, Nov 23 2017, 16:37:01) \\n[GCC 5.4.0 20160609] on linux' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True) 
      • Python 3.6.4 x86
         >>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.6.4 (default, Apr 25 2018, 23:55:56) \\n[GCC 5.4.0 20160609] on linux' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffff', False) 
    • Win x64 10 :
      • Python 3.5.4 x64
         >>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffffffffffff', True) 
      • Python 3.6.2 x86
         >>> import sys >>> "Python {0:s} on {1:s}".format(sys.version, sys.platform) 'Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32' >>> hex(sys.maxsize), sys.maxsize > 0x100000000 ('0x7fffffff', False) 


  1. Use [Python 3.Docs]: structure. CalcSize ( the format ) is determined (pointer) generated by the format object size. In other words, determining the size of the pointer sizeof(void*)( ):
    • OSX 9 x64
      • Python 2.7.10 x64
         >>> import struct >>> struct.calcsize("P") * 8 64 
    • Ubuntu 16 x64
      • Python 3.5.2 x64
         >>> import struct >>> struct.calcsize("P") * 8 64 
      • Python 3.6.4 x86
         >>> import struct >>> struct.calcsize("P") * 8 32 
    • Win x64 10 :
      • Python 3.5.4 x64
         >>> import struct >>> struct.calcsize("P") * 8 64 
      • Python 3.6.2 x86
         >>> import struct >>> struct.calcsize("P") * 8 32 


  1. Use [Python 3.Docs]: ctypes-Python external library . It comes down to determining the size of the pointer ( sizeof(void*)). Note, ctypes use # 2. (Not necessarily complete this task), by "$ {} PYTHON_SRC_DIR / Lib / ctypes / __ __.py the init" (the 15th line close to):
    • OSX 9 x64
      • Python 2.7.10 x64
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64 
    • Ubuntu 16 x64
      • Python 3.5.2 x64
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64 
      • Python 3.6.4 x86
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32 
    • Win x64 10 :
      • Python 3.5.4 x64
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64 
      • Python 3.6.2 x86
         >>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 32 


  1. [Python 3.Docs]: platform. Architecture ( executable file = sys.executable, bits = '' link = '' ) !!! in OSX unreliable on! Since the multi-architecture, an executable file (or .dylib ) format (in some cases, using # 2 ):
    • OSX 9 x64
      • Python 2.7.10 x64
         >>> import platform >>> platform.architecture() ('64bit', '') 
    • Ubuntu 16 x64
      • Python 3.5.2 x64
         >>> import platform >>> platform.architecture() ('64bit', 'ELF') 
      • Python 3.6.4 x86
         >>> import platform >>> platform.architecture() ('32bit', 'ELF') 
    • Win x64 10 :
      • Python 3.5.4 x64
         >>> import platform >>> platform.architecture() ('64bit', 'WindowsPE') 
      • Python 3.6.2 x86
         >>> import platform >>> platform.architecture() ('32bit', 'WindowsPE') 


  1. me foot solution ( gainarie ) - by [Python 3.Docs]: os call an external command ( [man7]: FILE (1) ) . System ( command ) . # 4 limitations . Applicable (sometimes not even use):
    • OSX 9 x64
      • Python 2.7.10 x64
         >>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /opt/OPSWbuildtools/2.0.6/bin/python2.7.global: Mach-O 64-bit executable x86_64 
    • Ubuntu 16 x64
      • Python 3.5.2 x64
         >>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /usr/bin/python3.5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=59a8ef36ca241df24686952480966d7bc0d7c6ea, stripped 
      • Python 3.6.4 x86
         >>> import os >>> os.system("file {0:s}".format(os.path.realpath(sys.executable))) /home/cfati/Work/Dev/Python-3.6.4/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=5c3d4eeadbd13cd91445d08f90722767b0747de2, not stripped 
    • Win x64 10 :
      • File tool does not exist, there are other possible use of third-party tools, but I would not stick with them


Specific win :

  1. By [Python 3.Docs] Check environment (e.g. % the PROCESSOR_ARCHITECTURE% (or other)) . Environment :
    • Win x64 10 :
      • Python 3.5.4 x64
         >>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'AMD64' 
      • Python 3.6.2 x86
         >>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'x86' 


  1. [Python 3.Docs]: sys. Version (also start explaining shown in the first row )
    • Check the # 1.

House # 14

platform.architecture()There is a problem (and expensive).

From Py2.6 start, easily tested sys.maxsize > 2**32.

This is a reliable test for the actual (default) size of a pointer, and at least from Py2.3: struct.calcsize('P') == 8beginning, it is compatible. In ctypes.sizeof(ctypes.c_void_p) == 8addition: .

Note: You can use gcc option -mx32or -mx32version constructed, they are 64-bit application architecture, but the default 32-bit pointer (save memory and speed). 'sys.maxsize = ssize_t' may not strictly represent a C pointer size (typically anyway 2**31 - 131-1). Furthermore, some of the different size of the code pointer and data systems, it is necessary to identify what the purpose of clarification, "32-bit or 64-bit mode" is?

Original articles published 0 · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/104277732