Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - check if a system is 32 or 64 bit to determine whether to run the function or not? [duplicate]

Possible Duplicate:
How do I determine if my python shell is executing in 32bit or 64bit mode?

I made a question earlier that never got replied to, but I have something more specific now so hopefully you can help.

Basically the SendKeys library only appears to install on my 32 bit system of Windows...

So I was wondering if there is a way of making it so this function I am going to write will only execute on a 32 bit system? I realise there is a platform.architecture() method to check the current system, but it returns the string "('64bit', 'WindowsPE')".

I was wondering if there was a way to read the 64 bit part of this string to make this function work correctly.

For example, pseudo code:

checker = platform.architecture()
system = strip or read 64 bit from checker string somehow
if system == 64 bit
then warn system is 64 bit and won't run function
else run function

Along the line of that. Unless there is a simpler way of checking it - maybe against the version of Python used (ie 32 or 64 bit)

Hope I've grasped this correctly - I'm still rather new to programming. :)

like image 448
Semaj Avatar asked Apr 01 '12 12:04

Semaj


People also ask

How do you check if you are running 32 or 64-bit Python?

Python Version Bit – Does My Python Shell Run 32 Bit or 64 Bit Version? To check which bit version the Python installation on your operating system supports, simply run the command “ python ” (without quotes) in your command line or PowerShell (Windows), terminal (Ubuntu, macOS), or shell (Linux).

How do I know if my system is 32-bit or 64-bit?

Click Start, type system in the search box, and then click System Information in the Programs list. When System Summary is selected in the navigation pane, the operating system is displayed as follows: For a 64-bit version operating system: X64-based PC appears for the System Type under Item.

Which version of Python do I have 32 or 64?

The most likely answer is 64-bit, for the following reasons: Most modern operating systems use a 64-bit edition of Python by default. Windows users can run 32-bit editions of Python on 64-bit Windows, but at a slight cost of performance. 32-bit Python, and 32-bit apps generally, can access only 4GB of memory at a time.

Can Python detect OS?

Detect OS Using the platform Module in Python This method returns information like name, release, and version of the current operating system, name of the machine on the network, and hardware identifier in the form of attributes of a tuple-like object.


2 Answers

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.

like image 134
Waynn Lue Avatar answered Oct 13 '22 22:10

Waynn Lue


An alternative method. Definitely works on all platforms:

import struct
is_64bit = struct.calcsize('P') * 8 == 64

As a note, this is part of its.py.

like image 31
jterrace Avatar answered Oct 13 '22 22:10

jterrace