What do I need to look at to see whether I'm on Windows or Unix, etc?
original title: "cross platform - Python: What OS am I running on?"
What do I need to look at to see whether I'm on Windows or Unix, etc?
Mit kell vizsgálnom, hogy Windows-on vagy Unix-on vagyok-e, stb.?
Ez az összefoglalás a fordítás után. Ha meg szeretné tekinteni a teljes fordítást, kattintson a "fordítás" ikonra
The output of
platform.system()
is as follows:Linux
Darwin
Windows
See: platform — Access to underlying platform’s identifying data
Dang -- lbrandy beat me to the punch, but that doesn't mean I can't provide you with the system results for Vista!
...and I can’t believe no one’s posted one for Windows 10 yet:
For the record here's the results on Mac:
Sample code to differentiate OS's using python:
You can also use sys.platform if you already have imported sys and you don't want to import another module
If you want user readable data but still detailed, you can use platform.platform()
Here's a few different possible calls you can make to identify where you are
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: https://github.com/hpcugent/easybuild/wiki/OS_flavor_name_version
Ubuntu 12.04 server for example gives:
I do this
Docs here : sys.platform.
Everything you need is probably in the sys module.
How about a new answer:
This would be the output if I was using MACOS
I am using the WLST tool that comes with weblogic, and it doesn't implement the platform package.
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:
/usr/bin/python3.2
For Jython the only way to get os name I found is to check
os.name
Java property (tried withsys
,os
andplatform
modules for Jython 2.5.3 on WinXP):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
sys.platform
is suffixed by kernel version, e.g.linux2
, everything else stays identicalplatform.architecture() = ('64bit', 'ELF')
WINDOWS (64bit)
(with 32bit column running in the 32bit subsystem)
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 :)
Interesting results on windows 8:
Edit: That's a bug
Watch out if you're on Windows with Cygwin where
os.name
isposix
.in the same vein....
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+
in python2.4
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.
try this:
and you can make it :
Check the available tests with module platform and print the answer out for your system:
You can also use only platform module without importing os module to get all the information.
A nice and tidy layout for reporting purpose can be achieved using this line:
That gives this output:
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:
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.This solution works for both
python
andjython
.module os_identify.py:
Use like this:
Use the
import os
andos.name
keywords.How about a simple Enum implementation like the following? No need for external libs!
Simply you can access with Enum value
P.S It is python3
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 byos, sys, platform, site
.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.)
I am late to the game but, just in case anybody needs it, this a function I use to make adjustments on my code so it runs on Windows, Linux and MacOs: