Check if Python interpreter is 32 or 64 bit

Like most other programs, 32-bit Python can be run on 64-bit OS. When needing to precisely detect if the operating system is 64-bit or 32-bit in a cross-platform way, one needs to check if Python itself is 32-bit to avoid falsely detecting that the operating system is 32 bit when the OS is actually 64 bit.

Note: This check is just for the Python interpreter. If you’re working on detecting the operating system parameters in a cross-platform-robust way, several further checks are necessary in general.

CLI example

python -c "import sys; print(sys.maxsize > 2**32)"
  • 32-bit Python will print False
  • 64-bit Python will print True

Functional

import sys

def is_64bit() -> bool:
    return sys.maxsize > 2**32

Notes

reference