views:

481

answers:

3

First of all, I don't think this question is a duplicate of
http://stackoverflow.com/questions/2208828/detect-64bit-os-windows-in-python
because imho it has not been thoroughly answered.

The only approaching answer is:

Use sys.getwindowsversion() or the existence of PROGRAMFILES(X86) (if 'PROGRAMFILES(X86)' in os.environ)

But:

  • Does the windows environment variable PROGRAMFILES(X86) reliable? I fear that anyone can create it, even if it's not present on the system.
  • How to use sys.getwindowsversion() in order to get the architecture?

Regarding sys.getwindowsversion():
The link http://docs.python.org/library/sys.html#sys.getwindowsversion
leads us to http://msdn.microsoft.com/en-us/library/ms724451%28VS.85%29.aspx
but I don't see anything related to the architecture (32bit/64bit).
Moreover, the platform element in the returned tuple seems to be independent of the architecture.

One last note: I'm looking for a solution using both python 2.5 and a windows version starting at Windows XP

Thanks!

Edit:
The relevant info is available here
http://msdn.microsoft.com/en-us/library/ms724340%28v=VS.85%29.aspx
but how can I get this with python?

Edit2: On a 64bit windows, with a 32bit python interpreter:

  • os.environ["PROCESSOR_ARCHITECTURE"] returns
    • 'x86'
  • platform.architecture() returns
    • ('32bit', 'WindowsPE')
A: 

Try the platform module - Access to underlying platform’s identifying data. Check the platform.machine() method.

On a 32-bit windows installation:

>>> import platform
>>> platform.architecture()
('32bit', 'WindowsPE')
>>> platform.machine()
'x86'
>>> 

On a 64-bit windows installation:

>>> import platform
>>> platform.architecture()
('64bit', 'WindowsPE')
>>> platform.version()
'5.2.3790'
>>> platform.machine()
'AMD64'
>>> 
gimel
Wrong: platform.architecture() gives the architecture of the python interpretor. If we install a 32bit version of python on a 64bit version of windows, platform.architecture() will return ('32bit', 'WindowsPE')Please see the comments of the first answer to the other SO question.
Thorfin
How about platform.machine() ?
gimel
returns an empty string
Thorfin
+1  A: 

1 Another option (poll WMI for OsArchitecture):

If you install pywin32 and the python wmi module on top you should be able to do (but only from Windows Vista and up!):

import wmi
c = wmi.WMI()
for os in c.Win32_OperatingSystem():
    print os.osarchitecture

2 Alternatively you could also use the _winreg module to check for the existence of SOFTWARE\Wow6432Node under HKEY_LOCAL_MACHINE (this is supposedly only there on 64 bit- OS versions) (no external dependencies).

ChristopheD
unfortunately, pywin32 is not available for both python 2.5 and amd64 architecture http://sourceforge.net/projects/pywin32/files/
Thorfin
Ok, added another option (2) - untested since I have no 64 bit windows OS around.
ChristopheD
Thanks Christophe, this should work indeed!But I prefer a bit Luke's solution because I don't have to import a windows specific module
Thorfin
+2  A: 
Luke
Sounds good! Do you know if a user can alter these environment variables (event unconsciously)?
Thorfin
Yes they can, using the SET command or the Windows UI.
Bastien Léonard