128

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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
  • 9
    the question marked as duplicate is targetted on OSX, this question is different. Vote to reopen – CharlesB May 29 '13 at 09:38
  • The question that this is apparently a duplicate of was closed 10 years ago. The Possible duplicate concerns macOS, not Windows. This should be reopened. – Xbox One Nov 23 '22 at 18:44
  • @XboxOne The duplicate includes answers that apply to Windows. – Mark Rotteveel Nov 26 '22 at 11:38
  • This absolutely should not be reopened, except to use the new duplicate closure system (as I just fixed now). The question does not fundamentally have anything to do with any particular OS, since the detection will be done within Python and since there is a simple, platform-agnostic answer. Besides, the OP for the canonical explicitly was open to answers for other platforms, and there is no good reason to scatter the information around. I also retagged both questions appropriately. – Karl Knechtel Jan 14 '23 at 09:30
  • The other version of the question is better, because on that one, the top and accepted answer is the platform-agnostic answer that also elegantly avoids a gotcha on MacOS. – Karl Knechtel Jan 14 '23 at 09:31

2 Answers2

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

Cristian
  • 42,563
  • 25
  • 88
  • 99
  • 5
    Not reliable... http://stackoverflow.com/a/12057504/156755 – Basic Aug 04 '16 at 23:34
  • 2
    Can somebody give an update for 2017 please. So confusing for noobs all that. Is `sys.maxsize` the right way to go today or does `platform.architecture()` works reliably on OS X, Win and Linux now? – Wlad Jan 31 '17 at 14:15
  • Nice, it worked. – hygull Jun 17 '18 at 05:29
  • 1
    If you want to check right in the command prompt run "python" command first, then "import platform;platform.architecture()" after ">>>". – mimic Jul 21 '18 at 23:24
  • `platform.architecture()` is problematic and expensive. Use `sys.maxsize > 2**32` since Py2.6. Or even more reliable and compatible at least since Py2.3: `struct.calcsize('P') == 8`. Or `ctypes.sizeof(ctypes.c_void_p) == 8`. There can be builds with gcc option `-mx32` or so, which are 64bit apps, but use 32bit pointers as default. 'sys.maxsize = ssize_t' may not strictly represent the C pointer size (its usually `2**31 - 1` anyway), there are systems which have different pointer sizes for code & data and it needs to be clarified what exactly is the purpose of "running as a 64-bit application?" – kxr Nov 07 '19 at 14:41
70

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)
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • Interesting gotcha. That smells a bit like a bug though. Is it supposed to work that way? – John La Rooy Dec 03 '09 at 20:44
  • 2
    I would consider it a bug. Looking at the code in the platform module, it seems to be a bit fragile and in this case it has to do with the way Apple implemented their multi-arch selection feature. I'm adding a note to ensure we look at this when the python.org OS X multi-arch selection feature is finalized. – Ned Deily Dec 03 '09 at 20:59
  • 1
    (I've also opened a bug with Apple.) – Ned Deily Dec 03 '09 at 21:44
  • 5
    On Windows x64 Python, `sys.maxint == 2147483647` so no dice there. That's because a C int on Windows is 32 bits for 32 and 64 bit. – David Heffernan Mar 21 '12 at 13:07
  • See http://stackoverflow.com/questions/1405913/how-do-i-determine-if-my-python-shell-is-executing-in-32bit-or-64bit-mode/1405971#1405971 for another approach to older Python versions that does not involve `sys.maxint`. – Ned Deily Sep 24 '12 at 16:07
  • I had both 32 and 64 bit of Python 27 installed on the same computer, and `sys.maxint` returned 2147483647 for both. The more accurate for me was `sys.maxsize` which differentiated correctly. Maybe `sys.maxsize` should be preferred for any version of Python? – Karim Bahgat Apr 16 '15 at 12:25
  • @KarimBahgat Thanks for the reminder. This answer needed to be updated a bit; `sys.maxsize` is indeed the right thing to use on modern Pythons including 2.6 and 2.7. – Ned Deily Apr 16 '15 at 18:54
  • `sys.maxsize` also has the advantage of being defined in 3.x; `sys.maxint` was removed, because the differentiation between `int` and `long` types was removed, and `int`s now store arbitrary-precision values. – Karl Knechtel Jan 14 '23 at 09:33