I need a way to tell the shell which mode it is in from the shell.
I try to see platform Module, but it only seems to tell you the information about "bit architecture and link format used by executable program": Although binary file is compiled to 64 bit (I run on OS X 10.6), so even if I use Here is an introduction. The 32-bit mode, which always seems to report 64 bits, is enforced by the.
#1 building
When you start the Python interpreter on the terminal / command line, 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
Where [MSC v.1500 64 bit (AMD64)] represents 64 bit Python. Applies to my specific settings.
#2 building
Update: one way is to look at sys.maxsize as a record Ad locum :
$ 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 to test for older systems, this slightly more complex test should apply to all Python versions 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 platform.architecture() for this. Unfortunately, the results are not always reliable, Especially in the case of OS X common 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
#3 building
Try ctypes to get the size of the void pointer:
import ctypes print ctypes.sizeof(ctypes.c_voidp)
It will be 4 for 32-bit and 8 for 64 bit.
#4 building
Basically a variation of Matthew Marshall's answer (with structure from the standard library):
import struct print struct.calcsize("P") * 8
#5 building
For non programming solutions, look at the activity monitor. It lists the architecture of 64 bit processes as "Intel (64 bit).".
#6 building
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 clicking python in cmd
#7 building
On my Centos Linux system, I did 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')
#8 building
platform.architecture() Notes:
Note: on Mac OS X (and possibly other platforms), executables can be generic files with multiple architectures.
In order to obtain the "64 bit" of the current interpreter, it is more reliable to query the sys.maxsize attribute:
import sys is_64bits = sys.maxsize > 2**32
#9 building
Open the python console:
import platform platform.architecture()[0]
It should display "64bit" or "32bit" depending on your platform.
Or ( For OS X binaries ):
import sys sys.maxsize > 2**32 # it should display True in case of 64bit and False in case of 32bit
#10 building
struct.calcsize("P") returns the size of bytes required to store a single pointer. On a 32-bit system, it will return four bytes. On a 64 bit system, it will return 8 bytes.
So here's 32, if you're running 32-bit Python and 64, if you're running 64 bit Python:
Python 2
import struct;print struct.calcsize("P") * 8
Python 3
import struct;print(struct.calcsize("P") * 8)
#11 building
import sys print(sys.version)
3.5.1 (v3.5.1:37a07cee5969, December 6, 2015, 01:54:25) [MSC v.1900 64 bit (AMD64))
#12 building
Platform architecture is not a reliable approach. 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)
#13 building
Group everything
In consideration of:
- Ask OSX questions (I have an old (cracked) VM with an old Python version)
- My main environment is Win
- I only installed 32-bit version on Win (and I built a "broken" version on Lnx)
I'll use Python 3 and Python 2 for examples on all three platforms.
- inspect [Python 3.Docs]: sys. maxsize Value - compare it to 0x100000000 (2 * * 32): larger for 64 bit, smaller for 32 bit:
- 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)
- Python 2.7.10 x64 :
- 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)
- Python 3.5.2 x64 :
- Win 10 x64:
- 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)
- Python 3.5.4 x64 :
- OSX 9 x64 :
- Use [Python 3.Docs]: structure. calcsize ( format ) Determines the size of the object resulting from the (pointer) format. In other words, determine the pointer size (sizeof(void *)):
- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import struct >>> struct.calcsize("P") * 8 64
- Python 2.7.10 x64 :
- 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
- Python 3.5.2 x64 :
- Win 10 x64:
- Python 3.5.4 x64 :
>>> import struct >>> struct.calcsize("P") * 8 64
- Python 3.6.2 x86 :
>>> import struct >>> struct.calcsize("P") * 8 32
- Python 3.5.4 x64 :
- OSX 9 x64 :
- Use [Python 3.Docs]: ctypes Python's external function library . It also boils down to determining the size of the pointer (sizeof(void *)). Note that ctypes uses 2. (it is not necessary to complete this task), through "${Python ﹣ SRC ﹣ dir} / lib / ctypes / ﹣ init ﹣. py "(near line 15):
- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import ctypes >>> ctypes.sizeof(ctypes.c_void_p) * 8 64
- Python 2.7.10 x64 :
- 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
- Python 3.5.2 x64 :
- Win 10 x64:
- 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
- Python 3.5.4 x64 :
- OSX 9 x64 :
- [Python 3.Docs]: platform. Schema (executable = sys.executable, bit = ', link =') !! Unreliable on OSX! Due to the multi architecture executable (or. dylib) format (in some cases, use 2.) :
- OSX 9 x64 :
- Python 2.7.10 x64 :
>>> import platform >>> platform.architecture() ('64bit', '')
- Python 2.7.10 x64 :
- 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')
- Python 3.5.2 x64 :
- Win 10 x64:
- Python 3.5.4 x64 :
>>> import platform >>> platform.architecture() ('64bit', 'WindowsPE')
- Python 3.6.2 x86 :
>>> import platform >>> platform.architecture() ('32bit', 'WindowsPE')
- Python 3.5.4 x64 :
- OSX 9 x64 :
- gainarie: the solution of me foot [Python 3.Docs]: os Call external command( [man7]: FILE(1) ) . System (command) . Limitations of 4. Applicable (sometimes even unavailable):
- 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
- Python 2.7.10 x64 :
- 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
- Python 3.5.2 x64 :
- Win 10 x64:
- File tools don't exist, and there are other third-party tools available, but I won't stick with them
- OSX 9 x64 :
Specific win:
- adopt [Python 3.Docs] Check environment variables (for example% processor "architecture% (or other)) . Environmental Science :
- Win 10 x64:
- Python 3.5.4 x64 :
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'AMD64'
- Python 3.6.2 x86 :
>>> import os >>> os.environ["PROCESSOR_ARCHITECTURE"] 'x86'
- Python 3.5.4 x64 :
- Win 10 x64:
- [Python 3.Docs]: sys. Edition (also shown on the first line when you start interpretation)
- Check 1.
#14 building
platform.architecture() has problems (and is expensive).
Starting with Py2.6, you can easily test sys. Maxsize > 2 * * 32.
This is a reliable test of the actual (default) pointer size and is compatible, starting at least with Py2.3: struct.calcsize('P') == 8. In addition: ctypes. Sizeof (ctypes. C? Void? P) = = 8.
Note: you can build with the gcc options - mx32 or - mx32 versions, which are 64 bit architecture applications, but use 32-bit pointers by default (saving memory and speed). 'sys.maxsize = ssize_t' may not strictly represent the C pointer size (usually 2 * * 31 - 1 31-1 anyway). Moreover, some systems have different pointer sizes for code and data, so it is necessary to clarify the purpose of distinguishing "32-bit or 64 bit mode"?