What do I need to look at to see whether I\'m on Windows or Unix, etc?
This solution works for both python
and jython
.
module os_identify.py:
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():
try:
platform.linux_distribution()
return True
except:
return False
def is_windows():
try:
platform.win32_ver()
return True
except:
return False
def is_mac():
try:
platform.mac_ver()
return True
except:
return False
def name():
if is_linux():
return "Linux"
elif is_windows():
return "Windows"
elif is_mac():
return "Mac"
else:
return "<unknown>"
Use like this:
import os_identify
print "My OS: " + os_identify.name()
For the record here's the results on Mac:
>>> import os
>>> os.name
'posix'
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'8.11.1'
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'
Short Story
Use platform.system()
. It returns Windows
, Linux
or Darwin
(for OSX).
Long Story
There are 3 ways to get OS in Python, each with its own pro and cons:
Method 1
>>> import sys
>>> sys.platform
'win32' # could be 'linux', 'linux2, 'darwin', 'freebsd8' etc
How this works (source): Internally it calls OS APIs to get name of the OS as defined by OS. See here for various OS-specific values.
Pro: No magic, low level.
Con: OS version dependent, so best not to use directly.
Method 2
>>> import os
>>> os.name
'nt' # for Linux and Mac it prints 'posix'
How this works (source): Internally it checks if python has OS-specific modules called posix or nt.
Pro: Simple to check if posix OS
Con: no differentiation between Linux or OSX.
Method 3
>>> import platform
>>> platform.system()
'Windows' # for Linux it prints 'Linux', Mac it prints `'Darwin'
How this works (source): Internally it will eventually call internal OS APIs, get OS version-specific name like 'win32' or 'win16' or 'linux1' and then normalize to more generic names like 'Windows' or 'Linux' or 'Darwin' by applying several heuristics.
Pro: Best portable way for Windows, OSX and Linux.
Con: Python folks must keep normalization heuristic up to date.
Summary
platform.system()
.posix
or nt
then use os.name
.sys.platform
.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
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):
os.name posix
sys.platform linux
platform.system() Linux
sysconfig.get_platform() linux-x86_64
platform.machine() x86_64
platform.architecture() ('64bit', '')
sys.platform
is suffixed by kernel version, e.g. linux2
, everything else stays identicalplatform.architecture() = ('64bit', 'ELF')
(with 32bit column running in the 32bit subsystem)
official python installer 64bit 32bit
------------------------- ----- -----
os.name 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
----- ----- -----
os.name 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
----- ------------------------ ----------------------
os.name 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
------ ----- -----
os.name 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:
distutils.util.get_platform()
which is identical to `sysconfig.get_platformTo 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("os.name ", os.name)
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())