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

share|improve this question
2  
see (bugs.python.org/issue12326) for details! – arnkore Jan 18 '12 at 9:34
2  
Here's a related question: Check linux distro name. – blong Dec 8 '15 at 19:00

22 Answers 22

up vote 459 down vote accepted
>>> import os
>>> print os.name
posix
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.22-15-generic'

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

share|improve this answer
11  
Why should I prefer platform over sys.platform? – matth Nov 7 '16 at 14:25
2  
@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 Jun 9 '17 at 10:22
    
On mac os X, platform.system() always return "Darwin"? or is there other case possible? – baptiste chéné yesterday

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.

share|improve this answer

How about a new answer:

import psutil
psutil.OSX     #True
psutil.WINDOWS #False
psutil.LINUX   #False 

This would be the output if I was using OSX

share|improve this answer
1  
Probably the most elegant of all answers – jsphpl Dec 8 '17 at 8:37

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
share|improve this answer
1  
Is this sample code from any python module? This is the only answer that in fact answers the question. – kon psych Jan 15 '15 at 19:22
4  
For fuzzier results, ``_platform.startswith('linux') – Klaatu von Schlacker Feb 8 '16 at 3:51
1  
original answer as seen here stackoverflow.com/a/8220141/3286832 – Yannis May 23 '16 at 16:32
    
the only elegant answer here. – Michael IV Jan 8 at 21:07

Use the import os and os.name keywords.

share|improve this answer

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
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'Vista'

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

>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'10'
share|improve this answer
4  
Windows 7: platform.release() '7' – Hugo Apr 20 '15 at 12:27
2  
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?? – AUTO Jun 8 '17 at 13:35

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

>>> import platform
>>> platform.platform()
'Linux-3.3.0-8.fc16.x86_64-x86_64-with-fedora-16-Verne'

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

import platform
import sys

def linux_distribution():
  try:
    return platform.linux_distribution()
  except:
    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
""" % (
sys.version.split('\n'),
str(platform.dist()),
linux_distribution(),
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
platform.mac_ver(),
))

The outputs of this script ran on a few different systems is available here: https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version

share|improve this answer

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

>>> import platform
>>> platform.os.name
'posix'
>>> 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]
share|improve this answer

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 ...
share|improve this answer

For the record here's the results on Mac:

>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'8.11.1'
share|improve this answer

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

>>> import os, platform
>>> print os.name
posix
>>> print platform.system()
CYGWIN_NT-6.3-WOW
share|improve this answer

try this:

import os

os.uname()

and you can make it :

info=os.uname()
info[0]
info[1]
share|improve this answer
    
os.uname() no longer works as of python 3.5. – Prahlad Yeri Dec 16 '16 at 17:52
1  
Also os.uname() is not available on windows: docs.python.org/2/library/os.html#os.uname Availability: recent flavors of Unix. – ccpizza Oct 24 '17 at 20:26

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 os.name
java 
wls:/offline> import sys
wls:/offline> print sys.platform
'java1.5.0_11'

Apart from patching the system javaos.py (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( "os.name" )
  return "win" in os.lower()
share|improve this answer

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():
        try:
            result = getattr(platform, x)()
            print "platform."+x+": "+result
        except TypeError:
            continue
share|improve this answer

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]
6.0

in python2.4

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

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.

share|improve this answer

Interesting results on windows 8:

>>> import os
>>> os.name
'nt'
>>> import platform
>>> platform.system()
'Windows'
>>> platform.release()
'post2008Server'

Edit: That's a bug

share|improve this answer

I do this

import sys
print sys.platform

Docs here : sys.platform.

Everything you need is probably in the sys module.

share|improve this answer

For Jython the only way to get os name I found is to check os.name 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 os.name Java property"""
    ver = sys.platform.lower()
    if ver.startswith('java'):
        import java.lang
        ver = java.lang.System.getProperty("os.name").lower()
    print('platform: %s' % (ver))
    return ver
share|improve this answer

/usr/bin/python3.2

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)
share|improve this answer
2  
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 : stackoverflow.com/faq. – ForceMagic Nov 9 '12 at 22:03
    
Good answer, maybe even on par with the original answer. But you could explain why. – vgoff Nov 9 '12 at 22:04

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("./my_so_dll.so")
share|improve this answer
8  
This is problematic if you are on a Mac since platform.system() returns "Darwin" on a Mac and "Darwin".lower().find("win") = 3. – mishaF Apr 19 '13 at 15:10
>>> import platform
>>> platform.system()
share|improve this answer

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
'linux2'
share|improve this answer
    
Does on of the approaches have any advantages, besides having to or not to import another module? – matth Nov 7 '16 at 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 Dec 21 '16 at 9:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.