How do I check if I'm running on Windows in Python?

gman picture gman · Aug 25, 2009 · Viewed 169.1k times · Source

I found the platform module but it says it returns 'Windows' and it's returning 'Microsoft' on my machine. I notice in another thread here on stackoverflow it returns 'Vista' sometimes.

So, the question is, how do implemement?

if isWindows():
  ...

In a forward compatible way? If I have to check for things like 'Vista' then it will break when the next version of windows comes out.


Note: The answers claiming this is a duplicate question do not actually answer the question isWindows. They answer the question "what platform". Since many flavors of windows exist none of them comprehensively describe how to get an answer of isWindows.

Answer

Martin Beckett picture Martin Beckett · Aug 25, 2009

Python os module

Specifically for Python 3.6/3.7:

os.name: The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.

In your case, you want to check for 'nt' as os.name output:

import os

if os.name == 'nt':
     ...

There is also a note on os.name:

See also sys.platform has a finer granularity. os.uname() gives system-dependent version information.

The platform module provides detailed checks for the system’s identity.