How do I check if I'm running on Windows in Python? [duplicate]
Asked Answered
D

5

339

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 is_windows():
  ...

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 is_windows. They answer the question "what platform". Since many flavors of windows exist none of them comprehensively describe how to get an answer of isWindows.

Drucill answered 25/8, 2009 at 1:15 Comment(3)
Similar to #197430Roberto
"There should be one-- and preferably only one --obvious way to do it." Alas, python gives us at least three ways..Stenophagous
Nit: According to PEP8 you should prefer using the name is_windows over isWindows.Belgae
T
514

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.

Tenebrous answered 25/8, 2009 at 1:17 Comment(10)
'nt' is the value for windowsJeffreys
What does linux normally return? posix?Maxama
@AndiJay - yes, but should be easy enough to test!Tenebrous
@MartinBeckett - Not necessarily. You may not have a machine running Linux available.Bentham
@Bentham you don't need a machine ;) onlinegdb.com/HyhJEzdX4 ( Well maybe in 2015 )Fischer
It turns out that os.name is actually not a good way. You want to use platform.system() instead. Here's the detailed answer: https://mcmap.net/q/53315/-how-to-identify-which-os-python-is-running-onTaunt
"... 'posix', 'nt', 'java'" Of course, Windows can run java, so this quote in your answer shows that testing for 'nt' is a bad way to test whether you're running on Windows.Semirigid
What is the 'java' OS ? Linux and Windows make sense but Java isn't an OS, is it ?Ike
system.uname(), not os.uname() :)Labefaction
@Haltarys, there were some phones running in java, before android became popular, I used to install games as .jar files, not sure if its anything like that.Starter
E
88

Are you using platform.system?

 system()
        Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.

        An empty string is returned if the value cannot be determined.

If that isn't working, maybe try platform.win32_ver and if it doesn't raise an exception, you're on Windows; but I don't know if that's forward compatible to 64-bit, since it has 32 in the name.

win32_ver(release='', version='', csd='', ptype='')
        Get additional version information from the Windows Registry
        and return a tuple (version,csd,ptype) referring to version
        number, CSD level and OS type (multi/single
        processor).

But os.name is probably the way to go, as others have mentioned.


For what it's worth, here's a few of the ways they check for Windows in platform.py:
if sys.platform == 'win32':
#---------
if os.environ.get('OS','') == 'Windows_NT':
#---------
try: import win32api
#---------
# Emulation using _winreg (added in Python 2.0) and
# sys.getwindowsversion() (added in Python 2.3)
import _winreg
GetVersionEx = sys.getwindowsversion
#----------
def system():

    """ Returns the system/OS name, e.g. 'Linux', 'Windows' or 'Java'.    
        An empty string is returned if the value cannot be determined.   
    """
    return uname()[0]
Englacial answered 25/8, 2009 at 1:22 Comment(3)
On a 64 bit machine, with Windows 7 (64 bit OS) this is the output: Python 3.1.1 (r311:74483, Aug 17 2009, 16:45:59) [MSC v.1500 64 bit (AMD64)] on win32 >>> print(sys.platform) win32 >>> platform.win32_ver() ('post2008Server', '6.1.7100', '', 'Multiprocessor Free') Note that the build explicitly calls it win32.Unbending
Oops, I thought the output would have been formatted better. hope you can read it anyway.Unbending
This should be the accepted answer.Gilt
T
52

You should be able to rely on os.name.

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

edit: Now I'd say the clearest way to do this is via the platform module, as per the other answer.

Toastmaster answered 25/8, 2009 at 1:18 Comment(0)
B
43

in sys too:

import sys
# its win32, maybe there is win64 too?
is_windows = sys.platform.startswith('win')
Bistort answered 25/8, 2009 at 1:21 Comment(3)
I'm on 64 bit Windows and this gives me 'win32' :)Trinitarianism
@Trinitarianism I just noticed the same thing.Snaffle
I'm kind of certain it usually returns 'win32' regardless; at least I've only seen checks for that in code I read.Edna
R
22
import platform
is_windows = any(platform.win32_ver())

or

import sys
is_windows = hasattr(sys, 'getwindowsversion')
Randa answered 2/4, 2014 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.