Python: get Windows OS version and architecture

First of all, I donโ€™t think this question is a duplicate Detecting 64-bit OS (windows) in Python
because IMHO he did not receive a complete answer.

The only approaching answer:

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

But:

  • Is the Windows PROGRAMFILES(X86) environment variable reliable? I am afraid that anyone can create it, even if it is not present in the system.
  • How to use sys.getwindowsversion() to get architecture?

Regarding sys.getwindowsversion() :
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 architecture (32bit / 64bit).
Moreover, the platform element in the returned tuple does not appear to be architecture dependent.

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

Thanks!

Edit:
Relevant information 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: In 64bit windows with 32bit python interpreter:

  • os.environ ["PROCESSOR_ARCHITECTURE"] returns
    • 'x86'
  • the .architecture () platform returns
    • ('32bit', 'WindowsPE')
+6
python windows cpu-architecture
source share
4 answers

These variables display the current execution status in the 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 
+7
source share

I think the platform module is really the best way to get this information.

  >>> import platform >>> platform.platform() 'Windows-7-6.1.7601-SP1' platform.processor() 'Intel64 Family 6 Model 42 Stepping 7, GenuineIntel' 

I donโ€™t see where to get the hard answer to 32-bit windows from here, so I suggest the following:

  try: os.environ["PROGRAMFILES(X86)"] bits = 64 except: bits = 32 print "Win{0}".format(bits) 

or, if you need to know what flavor of Python you are using (since you can run x32 python under x64 Windows):

 x32 python x64 windows: >>> platform.architecture() ('32bit', 'WindowsPE') >>> sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]' x64 python x64 windows: >>> platform.architecture() ('64bit', 'WindowsPE') >>> sys.version '2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)]' 
+7
source share

1 Another option (WMI poll for OsArchitecture):

If you install pywin32 and python wmi on top you should be capable (but only from Windows Vista and above!):

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

2 Alternatively, you can also use the _winreg module to check the existence of SOFTWARE\Wow6432Node under HKEY_LOCAL_MACHINE (this is supposedly only there in versions of 64 bit OS) (without external dependencies).

+2
source share

Hope this can fix the problem. I tried this on my Windows 8.1 64 bit and returned an AMD64 value for me.

 import _winreg def get_registry_value(key, subkey, value): key = getattr(_winreg, key) handle = _winreg.OpenKey(key, subkey ) (value, type) = _winreg.QueryValueEx(handle, value) return value windowsbit = get_registry_value( "HKEY_LOCAL_MACHINE", "SYSTEM\\CurrentControlSet\Control\\Session Manager\\Environment", "PROCESSOR_ARCHITECTURE") print windowsbit 

just run this code if you are running on a 64 bit windows machine this will print AMD64

or if you are working on 32-bit, it will print AMD32

Hope this code helps solve this problem completely.

+1
source share

All Articles