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

Question:

I’m doing some work with the Windows registry. Depending on whether Python is running as 32-bit or 64-bit, certain key values will be different. How can I detect whether Python is running as a 64-bit application or as a 32-bit application? (I’m not interested in detecting 32-bit/64-bit Windows – just the Python platform.)

Asked By: Nick Bolton

||

Answers:

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.

Answered By: Cristian

While it may work on some platforms, be aware that platform.architecture is not always a reliable way to determine whether python is running in 32-bit or 64-bit. In particular, on some OS X multi-architecture builds, the same executable file may be capable of running in either mode, as the example below demonstrates. The quickest safe multi-platform approach is to test sys.maxsize on Python 2.6, 2.7, Python 3.x.

$ 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)
Answered By: Ned Deily
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.