Knowing whether your Python interpreter is running in 32-bit or 64-bit mode is important for various reasons, such as compatibility with libraries, memory usage, and performance.
This guide presents reliable and cross-platform methods to determine your Python's "bitness" both from the command line and within Python scripts.
Checking Bitness from the Command Line (Cross-Platform)
The most reliable and concise way to check your Python's bitness from the command line (works on Windows, macOS, and Linux) is to use the -c option to execute a short Python command directly:
python -c "import sys; print(sys.maxsize > 2**32)"python -c: This tells Python to execute the following string as a Python command. You might need to usepython3orpyinstead ofpythondepending on your system.import sys: Imports thesysmodule, which provides access to system-specific parameters and functions.print(sys.maxsize > 2**32): This is the core check.sys.maxsizereturns the maximum value a variable of typePy_ssize_tcan take. This value is significantly larger on 64-bit systems than on 32-bit systems. We compare it to2**32(which is 2 raised to the power of 32, a key boundary between 32-bit and 64-bit addressing).- If the output is
True, you're running 64-bit Python. - If the output is
False, you're running 32-bit Python.
- If the output is
Checking Bitness Within a Python Script
You can perform the same check within a Python script:
Using sys.maxsize (Recommended)
import sys
is_64bits = sys.maxsize > 2**32
print(is_64bits)
if is_64bits:
print('Python is running as 64-bit application')
else:
print('Python is running as 32-bit application')This is the method officially recommended in the Python documentation. It's concise, clear, and works reliably across all major operating systems.
Using struct.calcsize()
Another cross-platform method involves the struct module, which is used for packing and unpacking binary data:
import struct
print(struct.calcsize('P') * 8) # Output: 32 or 64
if struct.calcsize('P') * 8 == 64:
print('Python is running as 64-bit application')
else:
print('Python is running as 32-bit application')struct.calcsize('P'): This returns the size (in bytes) of a native pointer on your system.'P'is a format character representing a "void *" (a generic pointer). On 32-bit systems, pointers are 4 bytes; on 64-bit systems, they are 8 bytes.* 8: We multiply by 8 to convert from bytes to bits.
Windows-Specific Method (Informational)
On Windows, you can sometimes determine the bitness by inspecting the output when you start the Python interpreter in the command prompt:
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>Look for text similar to "[MSC v.1929 64 bit (AMD64)]". However, this is NOT a reliable method for use within a script; it's purely for informational purposes when interacting with the interpreter directly.
Why platform.architecture() is Unreliable on macOS (and some other platforms)
You might encounter examples using platform.architecture():
import platform
print(platform.architecture()[0]) # Output might be '64bit'