Detect 64bit OS (windows) in Python
platform
module -- Access to underlying platform’s identifying data
>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')
On 64-bit Windows, 32-bit Python returns:('32bit', 'WindowsPE')
And that means that this answer, even though it has been accepted, is incorrect. Please see some of the answers below for options that may work for different situations. Python: get windows OS version and architecture
These variables show your current runtime status on windows:
@rem Test environment using this table:
@rem
@rem Environment Variable 32bit Native 64bit Native WOW64
@rem PROCESSOR_ARCHITECTURE x86 AMD64 x86
@rem PROCESSOR_ARCHITEW6432 undefined undefined AMD64
@rem
Python: How to know if the OS/CPU is 64bits
Most information you are querying is determined by the wordsize of the interpreter, not the CPU.
Only platform.machine()
ignores this information; it is taken from the system uname -m
data instead, which is the recommended command to determine if your system is 64-bit for both Linux and OS X, and Windows provides the exact same information (Python uses the C uname()
function in all cases).
Either test for 64
in that string, or build a set of acceptable values:
'64' in platform.machine()
orplatform.machine() in {'x86_64', 'AMD64'}
How do I detect if Python is running as a 64-bit application?
import platform
platform.architecture()
From the Python docs:Queries the given executable (defaults
to the Python interpreter binary) for
various architecture information.Returns a tuple (bits, linkage) which
contain information about the bit
architecture and the linkage format
used for the executable. Both values
are returned as strings.
How do I determine if my python shell is executing in 32bit or 64bit?
One way is to look at sys.maxsize
as documented here:
$ python-32 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffff', False)
$ python-64 -c 'import sys;print("%x" % sys.maxsize, sys.maxsize > 2**32)'
('7fffffffffffffff', True)
On Windows, run the same commands formatted as follows:python -c "import sys;print(\"%x\" % sys.maxsize, sys.maxsize > 2**32)"
sys.maxsize
was introduced in Python 2.6. If you need a test for older systems, this slightly more complicated test should work on all Python 2 and 3 releases:$ python-32 -c 'import struct;print( 8 * struct.calcsize("P"))'
32
$ python-64 -c 'import struct;print( 8 * struct.calcsize("P"))'
64
BTW, you might be tempted to use platform.architecture()
for this. Unfortunately, its results are not always reliable, particularly in the case of OS X universal binaries.$ arch -x86_64 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit True
$ arch -i386 /usr/bin/python2.6 -c 'import sys,platform; print platform.architecture()[0], sys.maxsize > 2**32'
64bit False
How can i detect whether my os is 32 bit or 64 bit through python code
import platform
platform.architecture()
('32bit', 'WindowsPE')
On 64-bit Windows, 32-bit Python returns: ('32bit', 'WindowsPE') # Don't know why.
A good method is to write a function likedef check_os_type():
if platform.machine().endswith('64'):
return '64 bit'
if platform.machine().endswith('32'):
return '32 bit'
Python - check if a system is 32 or 64 bit to determine whether to run the function or not?
Following this documentation, try this code:
is_64bits = sys.maxsize > 2**32
Note: this can return an incorrect result if 32bit Python is running on a 64bit operating system. How to identify which OS Python is running on?
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'
The output of platform.system()
is as follows:- Linux:
Linux
- Mac:
Darwin
- Windows:
Windows
platform
— Access to underlying platform’s identifying data Checking if an executable is 32-bit or 64-bit with a python3 script on Windows/Linux
Detecting the 64-bitness of ELF binaries (i.e. Linux) is easy, because it's always at the same place in the header:
def is_64bit_elf(filename):
with open(filename, "rb") as f:
return f.read(5)[-1] == 2
I don't have a Windows system, so I can't test this, but this might work on Windows:def is_64bit_pe(filename):
import win32file
return win32file.GetBinaryType(filename) == 6
Related Topics
Python Spacing and Aligning Strings
Numpy: Find Index of the Elements Within Range
Python Argparse: Default Value or Specified Value
Why Is the Apt-Get Function Not Working in the Terminal on MAC Os X V10.9 (Mavericks)
Pandas Dataframe Aggregate Function Using Multiple Columns
Logisticregression: Unknown Label Type: 'Continuous' Using Sklearn in Python
How to Suppress or Capture the Output of Subprocess.Run()
Memoization Library for Python 2.7
Python Wildcard Search in String
Suppressing Scientific Notation in Pandas
A Good Way to Make Long Strings Wrap to Newline
Pandas Interpolate Within a Groupby
How to Release Memory After Creating Matplotlib Figures
What Is More 'Pythonic' for 'Not'