How Python platform.architecture Determines Bitness
Python's platform.architecture() function determines the
bit architecture and linkage format of an executable—defaulting to the
running Python interpreter—by inspecting the binary file directly or
falling back to pointer size calculations. Instead of querying the host
operating system kernel, it analyzes the executable file on disk using
system tools to extract binary format details and returns a tuple
containing the architecture string (such as '64bit' or
'32bit') and the linkage format (such as 'ELF'
or 'PE').
Executable Inspection
When called without arguments, platform.architecture()
defaults its target to sys.executable, which points to the
path of the running Python binary. The function relies on the underlying
system's binary analysis utilities rather than high-level OS APIs.
On Unix-like platforms (such as Linux and macOS), the function
typically spawns the system's file command via a subprocess
or pipe to analyze the binary header of the target executable. The
file utility inspects the magic numbers and header fields
of the file format (such as ELF on Linux or Mach-O on macOS) and
produces a text description. The platform module parses
this output using regular expressions to search for bitness indicators
(such as 32-bit or 64-bit) and executable
format identifiers.
On Windows systems, it inspects the Portable Executable (PE) headers of the file directly or infers information based on the executable's structure to detect whether it is a PE32 or PE32+ binary.
Determining the Linkage Format
The second element returned in the tuple represents the linkage type of the binary. This value is derived directly from the file header format discovered during the executable inspection:
- ELF (Executable and Linkable Format): Common on Linux, BSD, and modern Unix systems.
- PE (Portable Executable): The standard executable format used across Microsoft Windows.
- Mach-O: The standard binary architecture format on macOS.
If the file inspection command fails, is unavailable, or does not recognize the binary format, the linkage value is returned as an empty string or an empty placeholder.
The Fallback Mechanism
If the target executable does not exist on disk, cannot be read, or
the file command fails to yield usable information,
platform.architecture() falls back to Python's internal
runtime state.
In this scenario, it determines bitness by inspecting
sys.maxsize:
- If
sys.maxsize > 2**32, the runtime environment is treated as64bit. - If
sys.maxsize <= 2**32, the runtime environment is treated as32bit.
When this fallback path is triggered, the linkage information cannot be confirmed from the file header, so the linkage string remains empty.
Process Bitness vs. OS Bitness
Because platform.architecture() targets the Python
binary itself, it reflects the bitness of the current Python process,
not necessarily the bitness of the underlying operating system. For
example, running a 32-bit Python interpreter on a 64-bit operating
system will return ('32bit', ...) because the executable
itself is compiled as a 32-bit binary.
Additionally, on platforms that support universal or
multi-architecture binaries (such as macOS with universal binaries
containing both x86_64 and ARM64 slices), static file inspection can be
ambiguous or misleading. For this reason, the official Python
documentation recommends using sys.maxsize directly when
developers solely need to verify whether the running interpreter
operates in a 32-bit or 64-bit address space.