Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

macos

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.

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

like image 585
jkp Avatar asked Sep 10 '09 15:09

jkp


People also ask

How do you check if you are running 32 or 64-bit Python?

Python Version Bit – Does My Python Shell Run 32 Bit or 64 Bit Version? To check which bit version the Python installation on your operating system supports, simply run the command “ python ” (without quotes) in your command line or PowerShell (Windows), terminal (Ubuntu, macOS), or shell (Linux).

How can I tell if an executable is 64-bit?

If you are on Windows 7, on a Windows Explorer, right click on the executable and select Properties. At the properties window select the Compatibility tab. If under the Compatibility Mode section you see Windows XP, this is a 32 bit executable. If you see Windows Vista, it is 64 bit.


1 Answers

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 
like image 63
Ned Deily Avatar answered Sep 20 '22 08:09

Ned Deily