Last modified: Nov 01, 2024 By Alexander Williams

Python sys.platform: Detecting the Operating System

The Python sys.platform attribute is a quick and efficient way to check the operating system your Python code is running on.

It’s particularly helpful when creating scripts that must behave differently across Windows, macOS, or Linux systems. This article covers sys.platform, explaining its uses and giving examples.

What is sys.platform in Python?

The sys.platform attribute is a string that identifies the operating system. Using it allows you to adapt your Python code to the OS environment.

The sys module includes many system-level functions and variables, and getting started with sys module can help you understand its versatility.

How to Use sys.platform in Python

To use sys.platform, first import the sys module. Then, access sys.platform to check the OS in your script.


import sys

print("Operating system:", sys.platform)


# Example output on macOS
Operating system: darwin

Platform Identifiers

Python’s sys.platform returns specific values based on the OS:

  • Windows: Returns win32
  • macOS: Returns darwin
  • Linux: Returns linux

Using these identifiers, you can make your code platform-specific for tasks that require OS-based differentiation.

Example: Checking the OS to Execute System Commands

In some scripts, you might want to execute OS-specific commands. By combining sys.platform with os.system or os.popen, you can run commands only on certain platforms.

To learn about executing commands with os.system, check How to Use os.system in Python. You can also use os.popen in Python to execute commands and capture their output.


import sys
import os

if sys.platform == "win32":
    os.system("echo This is Windows")
elif sys.platform == "darwin":
    os.system("echo This is macOS")
elif sys.platform == "linux":
    os.system("echo This is Linux")


# Output on Linux
This is Linux

Why Use sys.platform Instead of os.name?

The os.name variable is also available for checking the OS type, but it offers less specific information. For instance, both Windows and macOS return the generic value posix.

In contrast, sys.platform provides detailed identifiers, making it more suitable when you need exact OS detection.

Using sys.platform in Cross-Platform Scripts

If you’re writing cross-platform scripts, using sys.platform is invaluable. It allows you to branch code based on the operating system to ensure compatibility.


import sys

def platform_check():
    if sys.platform == "win32":
        print("Running on Windows")
    elif sys.platform == "darwin":
        print("Running on macOS")
    elif sys.platform == "linux":
        print("Running on Linux")
    else:
        print("Unknown platform")

platform_check()


# Example output on macOS
Running on macOS

Conclusion

Python's sys.platform attribute is an effective tool for OS detection, which is essential in cross-platform scripting. By identifying the OS, sys.platform helps you tailor code to ensure compatibility and efficiency.

For more about Python’s system and OS functions, check our guide to the Python sys module and related commands.