- К вопросу определения разрядности Python и Windows
- Python 32 bit and 64 bit
- # Check if Python is running as 32-bit or 64-bit
- # Checking if Python is running 32 or 64-bit from inside a script
- # A Windows-specific approach
- # Don’t use platform.architecture on macOS
- # Check if Python is running in 32 or 64-bit using struct.calcsize()
- How To Check If Python Is 32 Or 64-bit Windows
- 1. How To Check If Python Is 32 Or 64-bit Windows.
- 1.1 Method 1.
- 1.2 Method 2.
К вопросу определения разрядности Python и Windows
В некоторых случаях определения разрядности Python недостаточно для принятия решения без выяснения
разрядности Windows, на которой он работает. 32-битный Python на 64-битной Windows — не редкость.
И вроде бы решения известны, но вот не чувствовал я в себе уверенности, применяя их.
Хотя в VirtualBox всё выглядело хорошо, но возможности проверить на реальном железе не было.
А тут как раз и подвернулась такая возможность. На подходящем железе c Intel Core 2 Duo сначала была
развёрнута Win7(32bit), затем — Win7(64bit). Ну и Python 3.8.6 (32bit/64bit).
Из надёрганного в Интернете по этой теме был слеплен скриптик для сбора данных.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import ctypes print('ctypes.sizeof(ctypes.c_void_p) * 8 =', ctypes.sizeof(ctypes.c_void_p) * 8) print('') import os print('os.name =', os.name) print('') import platform print('platform.architecture() =', platform.architecture()) print('platform.machine() =', platform.machine()) print('platform.processor() =', platform.processor()) print('platform.system() =', platform.system()) print('platform.uname() =', platform.uname()) print('platform.win32_ver() =', platform.win32_ver()) print('platform.win32_edition() =', platform.win32_edition()) print('') import struct print('struct.calcsize("P") * 8 =', struct.calcsize('P') * 8) print('') import sys print('hex(sys.maxsize) =', hex(sys.maxsize)) print('(sys.maxsize > 0x100000000) is', sys.maxsize > 0x100000000) print('sys.platform =', sys.platform) print('sys.version() =', sys.version) print('sys.winver =', sys.winver) print('') import winreg key_reg = 'SOFTWARE\\Wow6432Node' is_exist = False try: reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) k = winreg.OpenKey(reg, key_reg) k.Close() is_exist = True except: pass print('HKEY_LOCAL_MACHINE\\ is exist?'.format(key_reg), is_exist)
И пускачи для Win64, поскольку питоны там ставились без ланчера и добавления путей в PATH.
@ECHO OFF SETLOCAL SET PATH=C:\Program Files (x86)\Python38-32\Scripts\;C:\Program Files (x86)\Python38-32\;%PATH% python.exe %* ENDLOCAL
@ECHO OFF SETLOCAL SET PATH=C:\Program Files\Python38\Scripts\;C:\Program Files\Python38\;%PATH% python.exe %* ENDLOCAL
И всё это было прогнано сначала на Win32, а затем на Win64.
Windows 7 (32bit), Python 3.8.6 (32bit):
C:\Tools>win_check.py ctypes.sizeof(ctypes.c_void_p) * 8 = 32 os.name = nt platform.architecture() = ('32bit', 'WindowsPE') platform.machine() = x86 platform.processor() = x86 Family 6 Model 15 Stepping 11, GenuineIntel platform.uname() = uname_result(system='Windows', node='usr-win32', release='7', version='6.1.7601', machine='x86', processor='x86 Family 6 Model 15 Stepping 11, GenuineIntel') platform.win32_ver() = ('7', '6.1.7601', 'SP1', 'Multiprocessor Free') platform.win32_edition() = Professional struct.calcsize("P") * 8 = 32 hex(sys.maxsize) = 0x7fffffff (sys.maxsize > 0x100000000) is False sys.platform = win32 sys.version() = 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)] sys.winver = 3.8-32 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node is exist? True
C:\Tools>py32 win_check.py ctypes.sizeof(ctypes.c_void_p) * 8 = 32 os.name = nt platform.architecture() = ('32bit', 'WindowsPE') platform.machine() = AMD64 platform.processor() = Intel64 Family 6 Model 15 Stepping 11, GenuineIntel platform.uname() = uname_result(system='Windows', node='usr-win64', release='7', version='6.1.7601', machine='AMD64', processor='Intel64 Family 6 Model 15 Stepping 11, GenuineIntel') platform.win32_ver() = ('7', '6.1.7601', 'SP1', 'Multiprocessor Free') platform.win32_edition() = Professional struct.calcsize("P") * 8 = 32 hex(sys.maxsize) = 0x7fffffff (sys.maxsize > 0x100000000) is False sys.platform = win32 sys.version() = 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)] sys.winver = 3.8-32 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node is exist? True
C:\Tools>py64 win_check.py ctypes.sizeof(ctypes.c_void_p) * 8 = 64 os.name = nt platform.architecture() = ('64bit', 'WindowsPE') platform.machine() = AMD64 platform.processor() = Intel64 Family 6 Model 15 Stepping 11, GenuineIntel platform.uname() = uname_result(system='Windows', node='usr-win64', release='7', version='6.1.7601', machine='AMD64', processor='Intel64 Family 6 Model 15 Stepping 11, GenuineIntel') platform.win32_ver() = ('7', '6.1.7601', 'SP1', 'Multiprocessor Free') platform.win32_edition() = Professional struct.calcsize("P") * 8 = 64 hex(sys.maxsize) = 0x7fffffffffffffff (sys.maxsize > 0x100000000) is True sys.platform = win32 sys.version() = 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] sys.winver = 3.8 HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node is exist? True
Ну и, по итогам, для уверенного опредения размерностей Python и Windows вроде бы
напрашивается соблюдение таких необходимых и достаточных условий:
if sys.platform == 'win32' and platform.machine() == 'x86': print('Windows (32bit) and Python (32bit)') elif sys.platform == 'win32' and sys.maxsize > 0x100000000: print('Windows (64bit) and Python (64bit)') elif sys.platform == 'win32': print('Windows (64bit) and Python (32bit)') else: print('This is NOT Windows. ')
Python 32 bit and 64 bit
Last updated: Feb 20, 2023
Reading time · 2 min
# 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.
Copied!# 👇️ 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)"
# 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.
Copied!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.
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.
# 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.
# 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.
Copied!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.
Copied!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.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
How To Check If Python Is 32 Or 64-bit Windows
When you run a python program on Windows, you may want to know whether the Python version is 32-bit to 64-bit, because some issues may happen when you run the python program with the wrong python bit version. This article will tell you how to check whether you python is 32-bit or 64 bit after you install it.
1. How To Check If Python Is 32 Or 64-bit Windows.
There are 2 methods to check if python is 32 or 64-bit version on windows.
1.1 Method 1.
- Input the keyword cmd in the Windows OS Type here to search text box.
- Press Enter key to open a Dos window.
- Input the command python in the dos command line, press Enter key to run it.
- Then it will display some Python-related information. From the first text line, we can see the python is the 32-bit version ( MSC v.1915 32 bit (Intel) ).
>python Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
Python 3.8.3 (default, Jul 2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
1.2 Method 2.
# First import struct module. >>> import struct >>> # Return 64 means 64-bit version, return 32 means 32-bit version. >>> version = struct.calcsize("P")*8 >>> >>> print(version) 64