Which Command to Use for Checking Whether Python Is 64Bit or 32Bit

Which command to use for checking whether python is 64bit or 32bit

For Python 2.6 and above, you can use sys.maxsize as documented here:

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

UPDATE: I notice that I didn't really answer the question posed. While the above test does accurately tell you whether the interpreter is running in a 32-bit or a 64-bit architecture, it doesn't and can't answer the question of what is the complete set of architectures that this interpreter was built for and could run in. As was noted in the question, this is important for example with Mac OS X universal executables where one executable file may contain code for multiple architectures. One way to answer that question is to use the operating system file command. On most systems it will report the supported architectures of an executable file. Here's how to do it in one line from a shell command line on most systems:

file -L $(python -c 'import sys; print(sys.executable)')

Using the default system Python on OS X 10.6, the output is:

/usr/bin/python: Mach-O universal binary with 3 architectures
/usr/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/python (for architecture i386): Mach-O executable i386
/usr/bin/python (for architecture ppc7400): Mach-O executable ppc

On one Linux system:

/usr/bin/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, stripped

BTW, here's an example of why platform is not reliable for this purpose. Again using the system Python on OS X 10.6:

$ 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

How do I detect if Python is running as a 64-bit application?

import platform
platform.architecture()

From the Python docs:

Queries the given executable (defaults
to the Python interpreter binary) for
various architecture information.

Returns a tuple (bits, linkage) which
contain information about the bit
architecture and the linkage format
used for the executable. Both values
are returned as strings.

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

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)

On Windows, run the same commands formatted as follows:

python -c "import sys;print(\"%x\" % sys.maxsize, sys.maxsize > 2**32)"

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

Checking if an executable is 32-bit or 64-bit with a python3 script on Windows/Linux

Detecting the 64-bitness of ELF binaries (i.e. Linux) is easy, because it's always at the same place in the header:

def is_64bit_elf(filename):
with open(filename, "rb") as f:
return f.read(5)[-1] == 2

I don't have a Windows system, so I can't test this, but this might work on Windows:

def is_64bit_pe(filename):
import win32file
return win32file.GetBinaryType(filename) == 6

Python - check if a system is 32 or 64 bit to determine whether to run the function or not?

Following this documentation, try this code:

is_64bits = sys.maxsize > 2**32

Note: this can return an incorrect result if 32bit Python is running on a 64bit operating system.

How to check if the installed Anaconda is 32-bit or 64-bit?

conda info has this information. If you need to access it programmatically, use conda info --json.

Which version of Python do I have installed?

python -V

http://docs.python.org/using/cmdline.html#generic-options

--version may also work (introduced in version 2.5)

In Python, how do you determine whether the kernel is running in 32-bit or 64-bit mode?

How about working around issue7860

import os
import sys
import platform

def machine():
"""Return type of machine."""
if os.name == 'nt' and sys.version_info[:2] < (2,7):
return os.environ.get("PROCESSOR_ARCHITEW6432",
os.environ.get('PROCESSOR_ARCHITECTURE', ''))
else:
return platform.machine()

def os_bits(machine=machine()):
"""Return bitness of operating system, or None if unknown."""
machine2bits = {'AMD64': 64, 'x86_64': 64, 'i386': 32, 'x86': 32}
return machine2bits.get(machine, None)

print (os_bits())

Detect 64bit OS (windows) in Python

platform module -- Access to underlying platform’s identifying data

>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')

On 64-bit Windows, 32-bit Python returns:

('32bit', 'WindowsPE')

And that means that this answer, even though it has been accepted, is incorrect. Please see some of the answers below for options that may work for different situations.



Related Topics



Leave a reply



Submit