308

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?

Perry
3,90326 gold badges40 silver badges51 bronze badges
asked Nov 21, 2011 at 23:40
2

6 Answers 6

559

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
23.2k7 gold badges64 silver badges111 bronze badges
answered Nov 21, 2011 at 23:45
Sign up to request clarification or add additional context in comments.

8 Comments

Note that in cygwin, it returns "cygwin" not "win32" as someone might expect.
Thanks. What's the difference between linux and linux2 ?
Note that since Python 3.3, "linux2" is no longer a possible value of platform (see the linked docs for corroboration) and so if you only need to support Python 3.3 and later you can safely delete the ` or platform == "linux2"` clause from the first condition.
why is it win32 not win64?
@ohailolcat , for backward compatibility. It will probably never change. See the original patch that defined it this way for more info: mail.python.org/pipermail/patches/2000-May/000648.html
|
61

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
159k94 gold badges435 silver badges477 bronze badges
answered Oct 8, 2017 at 8:38

4 Comments

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.devdocs.io/python~3.7/library/platform#platform.system
@BrandonBenefield, the enumeration is an example of possible values. On Apple devices, it returns "Darwin".
"The platform.system function uses uname internally." that's for Linux, what about Windows?
@Shayan For Windows, it basically is a formatter to sys.platform
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
7,1663 gold badges42 silver badges60 bronze badges
answered Nov 21, 2011 at 23:43

1 Comment

os.uname() returns an error on windows: >>> os.uname() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'os' has no attribute 'uname'. Did you mean: 'name'?
7

More detailed information are available in the platform module.

answered Nov 22, 2011 at 0:09

2 Comments

Does the platform module have any advantage over sys.platform? When would I want to use which approach?
@matth: You get more detailed, structured information from the platform module. Just click the link for documentation.
5

You can use sys.platform.

answered Nov 21, 2011 at 23:42

Comments

-1

I stumbled across this for all Kivy developers kivy.utils:

from kivy.utils import platform
if platform == 'linux':
 do_linux_things()
jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
answered Mar 3, 2024 at 16:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.