277

I want to check the operating system (on the computer where the script runs).

I know I can use os.system('uname -o') in Linux, but it gives me a message in the console, and I want to write to a variable.

It will be okay if the script can tell if it is Mac, Windows or Linux. How can I check it?

pppery
  • 3,731
  • 22
  • 33
  • 46
kolek
  • 3,690
  • 3
  • 24
  • 31
  • 3
    possible duplicate of [How can I tell What OS I am running on from Python?](http://stackoverflow.com/questions/1854/how-can-i-tell-what-os-i-am-running-on-from-python) – BoltClock Nov 22 '11 at 09:52
  • 1
    Possible duplicate of [Python: What OS am I running on?](https://stackoverflow.com/questions/1854/python-what-os-am-i-running-on) – TylerH Feb 24 '19 at 16:43

5 Answers5

516

You can use sys.platform:

from sys import platform
if platform == "linux" or platform == "linux2":
    # linux
elif platform == "darwin":
    # OS X
elif platform == "win32":
    # Windows...

sys.platform has finer granularity than sys.name.

For the valid values, consult the documentation.

See also the answer to “What OS am I running on?”

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
the wolf
  • 34,510
  • 13
  • 53
  • 71
54

If you want to know on which platform you are on out of "Linux", "Windows", or "Darwin" (Mac), without more precision, you should use:

>>> import platform
>>> platform.system()
'Linux'  # or 'Windows'/'Darwin'

The platform.system function uses uname internally.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • 2
    I like this solution but I want to point out that from the docs it states that it will return `Linux`, `Windows`, `Java` or an empty string.https://devdocs.io/python~3.7/library/platform#platform.system – Brandon Benefield Sep 05 '18 at 04:18
  • 4
    @BrandonBenefield, the enumeration is an example of possible values. On Apple devices, it returns “Darwin”. – Laurent LAPORTE Sep 05 '18 at 04:30
16

You can get a pretty coarse idea of the OS you're using by checking sys.platform.

Once you have that information you can use it to determine if calling something like os.uname() is appropriate to gather more specific information. You could also use something like Python System Information on unix-like OSes, or pywin32 for Windows.

There's also psutil if you want to do more in-depth inspection without wanting to care about the OS.

Rob Watts
  • 6,866
  • 3
  • 39
  • 58
Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
  • os.uname() returns an error on windows: >>> os.uname() Traceback (most recent call last): File "", line 1, in AttributeError: module 'os' has no attribute 'uname'. Did you mean: 'name'? – kristianp Jun 13 '23 at 04:26
7

More detailed information are available in the platform module.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Does the `platform` module have any advantage over `sys.platform`? When would I want to use which approach? – matth Nov 07 '16 at 14:42
  • @matth: You get more detailed, structured information from the `platform` module. Just click the link for documentation. – Sven Marnach Nov 07 '16 at 20:36
5

You can use sys.platform.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126