Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: get windows OS version and architecture

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')
like image 637
Thorfin Avatar asked May 04 '10 10:05

Thorfin


People also ask

How do I get Python os version on Windows?

To check which version of the Python library os is installed, run pip show os or pip3 show os in your CMD/Powershell (Windows), or terminal (macOS/Linux/Ubuntu).

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.

What is os name NT Python?

'nt' means 'New Technology' which is come with the release of a 32-bit version initially.


2 Answers

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)]'
like image 108
Curtis Price Avatar answered Sep 28 '22 22:09

Curtis Price


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
like image 28
Luke Avatar answered Sep 28 '22 21:09

Luke