Python: What OS am I running on?

Go To


What do I need to look at to see if I'm on Windows, Unix, etc?

2008-08-05 03:23
by Mark Harrison
see ( for details - arnkore 2012-01-18 09:34
Here's a related question: Check linux distro name - blong 2015-12-08 19:00


>>> import os
>>> print
>>> import platform
>>> platform.system()
>>> platform.release()

The output of platform.system() is as follows:

  • Linux: Linux
  • Mac: Darwin
  • Windows: Windows

See: platform — Access to underlying platform’s identifying data

2008-08-05 03:27
by Louis Brandy
Why should I prefer platform over sys.platform - matth 2016-11-07 14:25
@matth Slightly more consistent output. i.e. platform.system() returns "Windows" instead of "win32". sys.platform also contains "linux2" on old versions of Python while it contains just "linux" on newer ones. platform.system() has always returned just "Linux" - erb 2017-06-09 10:22
On mac os X, platform.system() always return "Darwin"? or is there other case possible - baptiste chéné 2018-01-12 13:35
@baptistechéné see the link in my answer where I have listed a few different ways to identify what system you are on ran on a few diferent machines: - Jens Timmerman 2019-01-23 12:39
@baptistechéné, I know this has over an year since you asked, but as a comment won't hurt, I'll post it anyways :)

So, the reason behind it is because it shows the kernel name. The same way Linux (the kernel) distros have many names (Ubuntu, Arch, Fedora among others), but it'll present itself as the kernel name, Linux. Darwin (a BSD-based Kernel), has its surrounding system, the macOS. I'm pretty sure apple did release Darwin as an open source code, but there's no other distro running over Darwin that I know of - Joao Paulo Rabelo 2019-01-30 12:05


Dang -- lbrandy beat me to the punch, but that doesn't mean I can't provide you with the system results for Vista!

>>> import os
>>> import platform
>>> platform.system()
>>> platform.release()

...and I can’t believe no one’s posted one for Windows 10 yet:

>>> import os
>>> import platform
>>> platform.system()
>>> platform.release()
2008-08-05 03:57
by Joey deVilla
Windows 7: platform.release() '7'Hugo 2015-04-20 12:27
So, yeah, I just ran platform.release() on my Windows 10, and it definitely just gave me '8'. Maybe I installed python before upgrading, but really? - Codesmith 2017-06-08 13:35
I'd have thought it's more likely you upgraded from Windows 8 (vs. it being a clean install) and whatever Python looks up in the registry or whatever was left behind - OJFord 2018-01-30 20:53
The release lookup for python on Windows appears to use the Win32 api function GetVersionEx at its core. The notes at the top of this Microsoft article about that function could be relevant: - theferrit32 2018-03-22 20:13


For the record here's the results on Mac:

>>> import os
>>> import platform
>>> platform.system()
>>> platform.release()
2008-08-05 04:13
by Mark Harrison


Sample code to differentiate OS's using python:

from sys import platform as _platform

if _platform == "linux" or _platform == "linux2":
   # linux
elif _platform == "darwin":
   # MAC OS X
elif _platform == "win32":
   # Windows
elif _platform == "win64":
    # Windows 64-bit
2014-09-16 07:42
by user3928804
Is this sample code from any python module? This is the only answer that in fact answers the question - kon psych 2015-01-15 19:22
For fuzzier results, ``_platform.startswith('linux' - Klaatu von Schlacker 2016-02-08 03:51
original answer as seen here - Yannis 2016-05-23 16:32
What kind of monster uses 3 spaces for tabs - Jason Floyd 2018-10-16 20:15
Mine returns win32 even though it is win64.. - Acecool 2018-11-01 13:31


You can also use sys.platform if you already have imported sys and you don't want to import another module

>>> import sys
>>> sys.platform
2008-08-26 15:41
by Moe
Does on of the approaches have any advantages, besides having to or not to import another module - matth 2016-11-07 14:41
Scoping is the main advantage. You want as few global variable names as possible. When you already have "sys" as a global name, you shouldn't add another one. But if you don't use "sys" yet, using "_platform" might be more descriptive and less likely to collide with another meaning - sanderd17 2016-12-21 09:01


If you want user readable data but still detailed, you can use platform.platform()

>>> import platform
>>> platform.platform()

Here's a few different possible calls you can make to identify where you are

import platform
import sys

def linux_distribution():
    return platform.linux_distribution()
    return "N/A"

print("""Python version: %s
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
mac_ver: %s
""" % (

The outputs of this script ran on a few different systems (Linux, Windows, Solaris, MacOS) and architectures (x86, x64, Itanium, power pc, sparc) is available here:

2013-01-23 10:55
by Jens Timmerman


I do this

import sys
print sys.platform

Docs here : sys.platform.

Everything you need is probably in the sys module.

2009-02-16 14:43
by ggambett


I am using the WLST tool that comes with weblogic, and it doesn't implement the platform package.

wls:/offline> import os
wls:/offline> print
wls:/offline> import sys
wls:/offline> print sys.platform

Apart from patching the system (issue with os.system() on windows 2003 with jdk1.5) (which I can't do, I have to use weblogic out of the box), this is what I use:

def iswindows():
  os = java.lang.System.getProperty( "" )
  return "win" in os.lower()
2010-06-11 07:37
by Alftheo


How about a new answer:

import psutil
psutil.MACOS   #True (OSX is deprecated)
psutil.WINDOWS #False
psutil.LINUX   #False 

This would be the output if I was using MACOS

2017-08-14 17:00
by whackamadoodle3000
psutil is not part of standard li - Corey Goldberg 2018-03-29 02:09
Those constants aren't showing up in my psutil? (python==3.6.5, psutil==3.3.0 - dwanderson 2018-09-14 01:07
Try using MACOS instead of OSX (which is deprecated - whackamadoodle3000 2018-09-14 03:04 - whackamadoodle3000 2018-09-14 03:04


>>> import platform
>>> platform.system()
2011-06-25 11:10
by NoName


For Jython the only way to get os name I found is to check Java property (tried with sys, os and platform modules for Jython 2.5.3 on WinXP):

def get_os_platform():
    """return platform name, but for Jython it uses Java property"""
    ver = sys.platform.lower()
    if ver.startswith('java'):
        import java.lang
        ver = java.lang.System.getProperty("").lower()
    print('platform: %s' % (ver))
    return ver
2013-01-09 08:47
by Michał Niklas
You can also call "platform.java_ver()" to extract OS information in Jython - DocOc 2018-10-03 16:08



def cls():
    from subprocess import call
    from platform import system

    os = system()
    if os == 'Linux':
        call('clear', shell = True)
    elif os == 'Windows':
        call('cls', shell = True)
2011-10-10 00:11
by urantialife
Welcome on SO, here, it is a good practice to explain why to use your solution and not just how. That will make your answer more valuable and help further reader to have a better understanding of how you do it. I also suggest that you have a look on our FAQ : - ForceMagic 2012-11-09 22:03
Good answer, maybe even on par with the original answer. But you could explain why - vgoff 2012-11-09 22:04


Watch out if you're on Windows with Cygwin where is posix.

>>> import os, platform
>>> print
>>> print platform.system()
2015-07-08 14:46
by guaka


Interesting results on windows 8:

>>> import os
>>> import platform
>>> platform.system()
>>> platform.release()

Edit: That's a bug

2013-02-14 22:44
by Eric


in the same vein....

import platform
is_windows=(platform.system().lower().find("win") > -1)

if(is_windows): lv_dll=LV_dll("my_so_dll.dll")
else:           lv_dll=LV_dll("./")
2011-09-28 17:54
by Elden
This is problematic if you are on a Mac since platform.system() returns "Darwin" on a Mac and "Darwin".lower().find("win") = 3 - mishaF 2013-04-19 15:10
is_windows = platform.system().lower().startswith("win") or Fals - Corey Goldberg 2018-03-29 02:05


If you not looking for the kernel version etc, but looking for the linux distribution you may want to use the following

in python2.6+

>>> import platform
>>> print platform.linux_distribution()
('CentOS Linux', '6.0', 'Final')
>>> print platform.linux_distribution()[0]
CentOS Linux
>>> print platform.linux_distribution()[1]

in python2.4

>>> import platform
>>> print platform.dist()
('centos', '6.0', 'Final')
>>> print platform.dist()[0]
>>> print platform.dist()[1]

Obviously, this will work only if you are running this on linux. If you want to have more generic script across platforms, you can mix this with code samples given in other answers.

2013-03-28 05:19
by sunil


try this:

import os


and you can make it :

2015-01-16 18:13
by NoName
os.uname() no longer works as of python 3.5 - Prahlad Yeri 2016-12-16 17:52
Also os.uname() is not available on windows: Availability: recent flavors of Unix.ccpizza 2017-10-24 20:26


Check the available tests with module platform and print the answer out for your system:

import platform

print dir(platform)

for x in dir(platform):
    if x[0].isalnum():
            result = getattr(platform, x)()
            print "platform."+x+": "+result
        except TypeError:
2014-10-30 00:43
by Stefan Gruenwald


You can also use only platform module without importing os module to get all the information.

>>> import platform
>>> platform.uname()
('Darwin', 'mainframe.local', '15.3.0', 'Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64', 'x86_64', 'i386')

A nice and tidy layout for reporting purpose can be achieved using this line:

for i in zip(['system','node','release','version','machine','processor'],platform.uname()):print i[0],':',i[1]

That gives this output:

system : Darwin
node : mainframe.local
release : 15.3.0
version : Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64
machine : x86_64
processor : i386

What is missing usually is the operating system version but you should know if you are running windows, linux or mac a platform indipendent way is to use this test:

In []: for i in [platform.linux_distribution(),platform.mac_ver(),platform.win32_ver()]:
   ....:     if i[0]:
   ....:         print 'Version: ',i[0]
2016-08-20 08:03
by G M


Use the import os and keywords.

2017-05-07 03:07
by nabu


If you are running macOS X and run platform.system() you get darwin because macOS X is built on Apple's Darwin OS. Darwin is the kernel of macOS X and is essentially macOS X without the GUI.

2018-01-13 21:29
by Alexander Calvert


import sys
import platform

# return a platform identifier

# return system/os name

# print system info
# similar to 'uname' command in unix
2019-02-03 22:43
by Yossarian42


I started a bit more systematic listing of what values you can expect using the various modules (feel free to edit and add your system):

Linux (64bit) + WSL                     posix
sys.platform                linux
platform.system()           Linux
sysconfig.get_platform()    linux-x86_64
platform.machine()          x86_64
platform.architecture()     ('64bit', '')
  • tried with archlinux and mint, got same results
  • on python2 sys.platform is suffixed by kernel version, e.g. linux2, everything else stays identical
  • same output on Windows Subsystem for Linux (tried with ubuntu 18.04 LTS), except platform.architecture() = ('64bit', 'ELF')

WINDOWS (64bit)

(with 32bit column running in the 32bit subsystem)

official python installer   64bit                     32bit
-------------------------   -----                     -----                     nt                        nt
sys.platform                win32                     win32
platform.system()           Windows                   Windows
sysconfig.get_platform()    win-amd64                 win32
platform.machine()          AMD64                     AMD64
platform.architecture()     ('64bit', 'WindowsPE')    ('64bit', 'WindowsPE')

msys2                       64bit                     32bit
-----                       -----                     -----                     posix                     posix
sys.platform                msys                      msys
platform.system()           MSYS_NT-10.0              MSYS_NT-10.0-WOW
sysconfig.get_platform()    msys-2.11.2-x86_64        msys-2.11.2-i686
platform.machine()          x86_64                    i686
platform.architecture()     ('64bit', 'WindowsPE')    ('32bit', 'WindowsPE')

msys2                       mingw-w64-x86_64-python3  mingw-w64-i686-python3
-----                       ------------------------  ----------------------                     nt                        nt
sys.platform                win32                     win32
platform.system()           Windows                   Windows
sysconfig.get_platform()    mingw                     mingw
platform.machine()          AMD64                     AMD64
platform.architecture()     ('64bit', 'WindowsPE')    ('32bit', 'WindowsPE')

cygwin                      64bit                     32bit
------                      -----                     -----                     posix                     posix
sys.platform                cygwin                    cygwin
platform.system()           CYGWIN_NT-10.0            CYGWIN_NT-10.0-WOW
sysconfig.get_platform()    cygwin-3.0.1-x86_64       cygwin-3.0.1-i686
platform.machine()          x86_64                    i686
platform.architecture()     ('64bit', 'WindowsPE')    ('32bit', 'WindowsPE')

Some remarks:

  • there is also distutils.util.get_platform() which is identical to `sysconfig.get_platform
  • anaconda on windows is same as official python windows installer
  • I don't have a Mac nor a true 32bit system and was not motivated to do it online

To compare with your system, simply run this script (and please append results here if missing :)

from __future__ import print_function
import os
import sys
import platform
import sysconfig

print("                      ",
print("sys.platform                 ",  sys.platform)
print("platform.system()            ",  platform.system())
print("sysconfig.get_platform()     ",  sysconfig.get_platform())
print("platform.machine()           ",  platform.machine())
print("platform.architecture()      ",  platform.architecture())
2019-02-23 02:39
by coldfix
Nice summary, but what does WSL say - not2qubit 2019-03-01 23:30
oh I totally forgot about WSL, added now (basically same as linux - coldfix 2019-03-02 00:35


How about a simple Enum implementation like the following? No need for external libs!

import platform
from enum import Enum
class OS(Enum):
    def checkPlatform(osName):
        return osName.lower()== platform.system().lower()

    MAC = checkPlatform("darwin")
    LINUX = checkPlatform("linux")
    WINDOWS = checkPlatform("windows")  #I haven't test this one

Simply you can access with Enum value

if OS.LINUX.value:
    print("Cool it is Linux")

P.S It is python3

2018-09-27 17:39
by Memin


This solution works for both python and jython.


import platform
import os

# This module contains functions to determine the basic type of
# OS we are running on.
# Contrary to the functions in the `os` and `platform` modules,
# these allow to identify the actual basic OS,
# no matter whether running on the `python` or `jython` interpreter.

def is_linux():
        return True
        return False

def is_windows():
        return True
        return False

def is_mac():
        return True
        return False

def name():
    if is_linux():
        return "Linux"
    elif is_windows():
        return "Windows"
    elif is_mac():
        return "Mac"
        return "<unknown>" 

Use like this:

import os_identify

print "My OS: " +
2019-01-29 13:06
by hoijui


You can look at the code in pyOSinfo which is part of the pip-date package, to get the most relevant OS information, as seen from your Python distribution.

One of the most common reasons people want to check their OS is for terminal compatibility and if certain system commands are available. Unfortunately, the success of this checking is somewhat dependent on your python installation and OS. For example, uname is not available on most Windows python packages. The above python program will show you the output of the most commonly used built-in functions, already provided by os, sys, platform, site.

enter image description here

So the best way to get only the essential code is looking at that as an example. (I guess I could have just pasted it here, but that would not have been politically correct.)

2019-02-07 21:10
by not2qubit


Just for completeness, "OS" environment variable seems to be defined everywhere. On Windows XP/7/8/10 it is set to "Windows_NT". On Linux SuSE SP2 it is set to "x86-64 linux sles11[2]". I don't have access to OS-X or BSD machines, would be interesting to check there as well.

import os

os_name = os.getenv("OS")
if os_name == "Windows_NT":
    # Windows
elif "linux" in os_name:
    # Linux
elif ...
2015-12-01 07:28
by Alexey Polonsky