How to Find the Operating System in Python

Published September 22, 2025

This blog post will show you different ways to find the operating system in Python.

Why would you need to find the operating system name in Python?

I have a few scripts that run cross-platform, on the macOS, Debian Linux and Raspberry Pi OS. A part of the program includes OS-specific code.

For example, if the program is running on macOS, I want it to run Mac-specific osascript command and prompt me with an alert.

This is how I would do it.

 if sys.platform  != 'darwin':
        osascript = f"""osascript -e 'display notification "Activity Completed" with title "This activity was completed successfully."'"""

Find operating system name in Python Python program to find which operating system is being used

Use os.uname() to find the operating system

Import the os module and call the os.uname() method to find the operating system name.

On Python shell, run this:

import os

os.uname()[1]

Output on Debian Linux:

>>> os.uname()[0]
'Linux'

Output on MacBook Pro with M4 processor:

>>> os.uname()[0]
'Darwin'

Use sys.platform to find the operating system

Import the sys module and call the sys.platform attribute to find the operating system name.

On Python shell, run this:

import sys

sys.platform

Output on Debian Linux:

>>> sys.platform
'linux'

Output on MacBook :

>>> sys.platform
'darwin'

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: September 22, 2025.     This post was originally written on September 22, 2025.