Last updated: Apr 9, 2024
Reading timeยท2 min
Use the python -c "import sys; print(sys.maxsize > 2**32)"
command to check if
Python is running as 32-bit or 64-bit.
The command will return True
if Python is running in 64-bit and False
if
it's running in 32-bit.
# ๐๏ธ returns True if Python interpreter is running in 64-bit # works on Linux, macOS and Windows python -c "import sys; print(sys.maxsize > 2**32)"
You can use the same approach to check if the Python interpreter is running 32-bit or 64-bit from inside a script.
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')
The sys.maxsize > 2**32
expression returns True
if the Python interpreter
runs as 64-bit and False
if it runs as 32-bit.
This is the approach that the documentation recommends.
The sys.maxsize
attribute returns an integer that is usually 2**31 - 1
on a 32-bit platform
and 2**63 - 1
on a 64-bit platform.
sys.maxsize
attribute returns a value greater than 2**32
, then the Python interpreter is running as 64-bits.This approach works on Windows, macOS and Linux.
If you are on Windows, you can also start your shell and look at the message to check if your Python is running as a 32-bit or 64-bit application.
platform.architecture
on macOSSome examples online might use the platform.architecture method to check if the Python interpreter is running as 32 or 64-bit.
import platform print(platform.architecture()[0]) # ๐๏ธ 64bit
However, the output of the method is not reliable for macOS and some other platforms because executable files may be universal files that contain multiple architectures.
The sys.maxsize > 2**32
expression is reliable on all platforms.
You can also use the struct.calcsize() method to check if Python is running as a 32-bit or 64-bit application.
import struct print(struct.calcsize('P') * 8) # ๐๏ธ 64 if struct.calcsize('P') * 8 == 64: print('Python is running as 64-bit application') else: print('Python is running as 32-bit application')
The P
character represents void *
(a generic pointer).
The pointer is 4 bytes on 32-bit systems and 8 bytes on 64-bit systems.
The call to the struct.calcsize()
method calculates the number of bytes that
are required to store a single pointer.
The result will be 4
on a 32-bit system and 8
on a 64-bit system.
We multiply 4
or 8
by 8
to get the result as 32
or 64
bits.
Which approach you pick is a matter of personal preference. I'd use
sys.maxsize > 2**32
because it is recommended in the docs and works on all
operating systems.