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

0 votes
asked Sep 10, 2009 by jkp

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

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

11 Answers

0 votes
answered Sep 10, 2009 by ned-deily

UPDATED: 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"))'
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'

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 votes
answered Sep 10, 2009 by matthew-marshall

Try using ctypes to get the size of a void pointer:

import ctypes
print ctypes.sizeof(ctypes.c_voidp)

It'll be 4 for 32 bit or 8 for 64 bit.

0 votes
answered Sep 10, 2009 by christophed

Basically a variant on Matthew Marshall's answer (with struct from the std.library):

import struct
print struct.calcsize("P") * 8
0 votes
answered Sep 27, 2009 by peter-hosey

For a non-programmatic solution, look in the Activity Monitor. It lists the architecture of 64-bit processes as “Intel (64-bit)”.

0 votes
answered Sep 10, 2012 by dustin

When starting the Python interpreter in the terminal/command line you may also see a line like:

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)] means 64-bit Python. Works for my particular setup.

0 votes
answered Sep 5, 2014 by nikhil

Python 2.7.6 (default, Nov XY ..., 19:24:24) **[MSC v.1500 64 bit (AMD64)] on win
Type "help", "copyright", "credits" or "license" for more information.

after hitting python in cmd

0 votes
answered Sep 2, 2015 by rekabbnad

On my centos linux platform I did the following:

1) start python interpreter (I'm using 2.6.6)
2) import platform
3) print platform.architecture()

and it gave me (64bit, 'ELF')

0 votes
answered Sep 12, 2015 by shannon-mann

platform.architecture() notes say:

Note: On Mac OS X (and perhaps other platforms), executable files may be universal files containing multiple architectures.

To get at the “64-bitness” of the current interpreter, it is more reliable to query the sys.maxsize attribute:

is_64bits = sys.maxsize > 2**32
0 votes
answered Sep 17, 2016 by abe312

Open python console:

import platform

it should display the '64bit' or '32bit' according to your platform.

Alternatively( in case of OS X binaries ):

import sys
sys.maxsize > 2**32 
# it should display True in case of 64bit and False in case of 32bit
0 votes
answered Sep 29, 2016 by kimbaudi

struct.calcsize("P") returns size of the bytes required to store a single pointer. On a 32-bit system, it would return 4 bytes. On a 64-bit system, it would return 8 bytes.

So the following would return 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)
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter