Check if Python is running as 32-bit or 64-bit

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
2 min

banner

# Check if Python is running as 32-bit or 64-bit

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.

shell
# ๐Ÿ‘‡๏ธ 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)"

check if python interpreter 32 or 64 bit

# Checking if Python is running 32 or 64-bit from inside a script

You can use the same approach to check if the Python interpreter is running 32-bit or 64-bit from inside a script.

main.py
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')

check if python 32 or 64 bit from inside script

The code for this article is available on GitHub

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.

If the 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.

check if python 32 or 64 bit windows

# A Windows-specific approach

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.

windows check 32 or 64 bit python interpreter

The "[MSC v.1933 64 bit (AMD64)]" message shows that Python is running as a 64-bit application.

# Don't use platform.architecture on macOS

Some examples online might use the platform.architecture method to check if the Python interpreter is running as 32 or 64-bit.

main.py
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.

# Check if Python is running in 32 or 64-bit using struct.calcsize()

You can also use the struct.calcsize() method to check if Python is running as a 32-bit or 64-bit application.

main.py
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 code for this article is available on GitHub

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev