[Python]Is there a reliable (and cross-platform) way to find out system architecture?

Heya,

As the title says. I tried quite a lot of things and all i could come up with/find on the net are only able to tell the python interpreter’s architecture not the system architecture.

Regards,
Thorsten

Here’s the function we use to do that in our studio library. Not super sexy but it works perfectly.

import _winreg
def get_os_architecture():
   key = r'Software\Wow6432Node'
   # turns reg_string into required registry constant out of _winreg
   reg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
   try:
      k = _winreg.OpenKey(reg, key)
      _winreg.CloseKey(k)
      value = '64-bit'
   except EnvironmentError:
      # folder or value didn't exist, so...
      value = '32-bit'
   return value

we’ve been using this: (uses ctypes and the platform module)
We also want to know if a machine runs XP x64 or Win7/Vista x64. So far this solution worked for us very reliably.

    
if platform.release()=="2003Server":    # xp64    
   # you got a XP 64 system
elif is64BitSystem():                   # win7/vista x64        
   # you got Win7/Vista 64
else:
   # you run a x86 system (query platform module for more info)

def is64BitSystem:
   i = ctypes.c_int()
   kernel32 = ctypes.windll.kernel32
   process = kernel32.GetCurrentProcess()
   kernel32.IsWow64Process(process, ctypes.byref(i))
   if(i.value!=0):
      # you got a x64 system

[QUOTE=RobertKist;11196]we’ve been using this: (uses ctypes and the platform module)
We also want to know if a machine runs XP x64 or Win7/Vista x64. So far this solution worked for us very reliably.

    
def is64BitSystem:
   i = ctypes.c_int()
   kernel32 = ctypes.windll.kernel32
   process = kernel32.GetCurrentProcess()
   kernel32.IsWow64Process(process, ctypes.byref(i))
   if(i.value!=0):
      # you got a x64 system

[/QUOTE]

This is an interesting pattern, out of curiosity, how different is the above function from just calling platform.architecture()?

platform.architecture( ) is inaccurate on Windows, at least in Python 2.6.x. Here’s what it does on my Win7 x64 system:

 >>> platform.architecture()
('32bit', 'WindowsPE')

Ah good point, you’d have to have a known executable to be able to reliably check against, tho if that’s the case, i wonder if you could just check against something like explorer.exe or some other windows system executable…of course that effectively destroys the cross platform requirement, or i guess you could have a dict or elifs that held different executables per OS…but then you’d have to maintain that i suppose.

Adam - you’re right. platform.architecture doesn’t work - I think it just reports the architecture of the environment the python interpreter runs in. If it’s running in a 32 bit env on a 64 bit machine it will report 32 bit. Use release() and system() instead.

That’s why our detection process has 2 steps. We want to know what kind of OS we’re using e.g. XP or newer or OS X (also use platform.system). Second step is to find out if it’s x64 or not (for windows). You can also throw in some bit depth detection code for OS X at this point but usually it’s just enough to know you’re not on Windows.

So far we didn’t have any issues with this. It’s just a different approach.
The platform module is pretty reliable for us here (on 2.6.1), except when determining the bits of the OS.

I guess you can also pull the windows version out of the registry somehow - after all, Python has to get it from somewhere.

Is Robert’s solution cross-platform? Never used the ctypes module.

You can check a lot of system architecture by checking environment variables. Var names will vary on windows and linux. On windows just type ‘set’ on the commandline to see all available system variables.

[QUOTE=Rob Galanakis;11203]Is Robert’s solution cross-platform? Never used the ctypes module.

You can check a lot of system architecture by checking environment variables. Var names will vary on windows and linux. On windows just type ‘set’ on the commandline to see all available system variables.[/QUOTE]

That’s sorta the lines i was thinking along, if you knew of a certain system executable per OS, you could check for the presence, then if you found it you could pass it to platform.architecture() and get the bit-depth.

Why check for a system executable though? I think the platform module works quite well in determining the actual OS (Win, Mac, Unix). It just has an issues with the bit depth, and for this you have to code platform specific routines. The platform module not 99.9999% reliable but good enough for most uses I think, when you just want the OS and not the bit depth.

I don’t think there’s an easy way to determine the bit depth universally (maybe check the default pointer size of the OS - 32/64 bit? but I guess that requires a kernel call which is platform specific again…). In win you can query the registry or the kernel32 process. On the Mac there’s probably some other way. And if you’re lucky, what works on the Mac may also work in other Unix flavors.

I haven’t done too much python coding on the mac though. python on the mac is a hassle imho, so I switched to Qt/C++. If you use Qt in Python (e.g. Maya), you can also use Qt’s functions to query the platform, which should be more robust than Python’s built in modules.

[QUOTE=RobertKist;11205]Why check for a system executable though?[/QUOTE]

I’d think that’s a pretty reliable way to get the bit-depth. I mean if you’re on a 64 bit system, wouldn’t the system level executables be 64-bit? If you pass ‘explorer.exe’ to platform.architecture() on a 64-bit OS it gives you back (‘64bit’,‘’), but again, seems like the second value is unnecessary anyway since you’ve already determined you’re on windows by the use of ‘explorer.exe’ (‘explorer.exe’ might not be the best example, but you get what i’m saying).