10

First of all, I don't think this question is a duplicate of
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')
5
  • What do you want to know, exactly? The size of the architecture or the place where programs are stored? Commented May 4, 2010 at 10:11
  • As the question title says: size of architecture and version Commented May 4, 2010 at 10:40
  • Tjese answeres are correct and the solution too, its just if you run an 32bit programm on 64 bit, windows emulates these data. So the winapi returns an incorrect version for you. Commented May 5, 2010 at 5:20
  • I'm looking for correct answers independently of the python interpreter target architecture. It's the OS architecture than I'm looking for. Commented May 5, 2010 at 8:23
  • architecture only subset: stackoverflow.com/questions/7491391/… 32 vs 64 subset: stackoverflow.com/questions/1405913/… Commented Nov 17, 2018 at 18:39

6 Answers 6

26

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

  >>> 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 a firm answer on 32/64 bit windows from here, so I suggest this:

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

or, if you need to know which flavor of Python you are running (as 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)]'
Sign up to request clarification or add additional context in comments.

2 Comments

platform.machine() now returns 32/64 bit as i386/AMD64. platform.uname() will return the OS version and architecture in a single call.
Python 3.10 for platform.machine() returns either x86, or AMD64 (in Windows 10)
6

These variables show your current runtime status on 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

1 Comment

Sounds good! Do you know if a user can alter these environment variables (event unconsciously)?
2

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).

3 Comments

unfortunately, pywin32 is not available for both python 2.5 and amd64 architecture sourceforge.net/projects/pywin32/files
Ok, added another option (2) - untested since I have no 64 bit windows OS around.
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
1

i hope this can solve the problem i tried it on my windows 8.1 64 bit and returns the value AMD64 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 working on 64 bit windows machine this will print AMD64

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

i hope this code can help to solve this problem fully

1 Comment

This is the same as getting an environment variable. The only difference is it isn't affected by a SET command.
0

Here's a nice Python command one-liner.

>python -c "import platform as p; print(p.platform());print(p.processor())"
Windows-10-10.0.17134-SP0
Intel64 Family 6 Model 61 Stepping 4, GenuineIntel

>

Comments

0

This post is heck old, but here's a little trick you can use to obtain the Windows OS arch. It's simple and reliable

import shutil
is_x64 = shutil.which("wow64win.dll") is not None
is_x32 = not is_x64
# or a one-liner
is_x32 = not (is_x64 := shutil.which("wow64win.dll") is not None)

This works because System32 is always on path, and all x64 distributions have the WoW64 translation layer for the 32-bit applications

As for the os version you can use the Curtis Price answer:

>>> import platform
>>> platform.platform()
"Windows-7-6.1.7601-SP1"

2 Comments

Is this true even for Windows on ARM/AArch64? I guess probably, if the emulation layers for 32-bit x86 and for x64 binaries use the same DLL names. But still, this only gives you one bit of information, so can't distinguish ARM64 from x86-64.
@PeterCordes The WoW64 also exists on 64-bit ARM builds. Docs here. WoW64.dll is the core, and WoW64Win.dll is the thunk loaded into the 32-bit process. It is possible to combine my previous answer with something like this: os.environ.get('PROCESSOR_ARCHITEW6432', os.environ.get('PROCESSOR_ARCHITECTURE', '')) That way, you can find the OS address width and the CPU architecture

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.