October 13, 2024

Python Platform Module

The platform module in Python is used to access various information about the underlying platform and operating system. This module provides functions to retrieve information such as the operating system name, version, machine architecture, and more.

1. Importing the Platform Module

To use the functions provided by the platform module, you first need to import it:

import platform

2. Key Functions in the Platform Module

2.1 platform.system()

Returns the name of the operating system:

import platform

# Get the operating system name
os_name = platform.system()
print(f"Operating System: {os_name}")

2.2 platform.version()

Returns the version of the operating system:

import platform

# Get the version of the operating system
os_version = platform.version()
print(f"OS Version: {os_version}")

2.3 platform.release()

Returns the operating system release version:

import platform

# Get the release version of the operating system
os_release = platform.release()
print(f"OS Release: {os_release}")

2.4 platform.machine()

Returns the machine type (e.g., ‘x86_64’, ‘i386’):

import platform

# Get the machine type
machine_type = platform.machine()
print(f"Machine Type: {machine_type}")

2.5 platform.processor()

Returns the processor name:

import platform

# Get the processor name
processor_name = platform.processor()
print(f"Processor: {processor_name}")

2.6 platform.platform()

Returns a single string identifying the underlying platform with version:

import platform

# Get a platform string
platform_info = platform.platform()
print(f"Platform Info: {platform_info}")

2.7 platform.uname()

Returns a named tuple with various attributes about the platform:

import platform

# Get detailed platform information
platform_details = platform.uname()
print(f"System: {platform_details.system}")
print(f"Node Name: {platform_details.node}")
print(f"Release: {platform_details.release}")
print(f"Version: {platform_details.version}")
print(f"Machine: {platform_details.machine}")
print(f"Processor: {platform_details.processor}")

3. Example: Displaying Full System Information

import platform

def display_system_info():
    print(f"Operating System: {platform.system()}")
    print(f"OS Version: {platform.version()}")
    print(f"OS Release: {platform.release()}")
    print(f"Machine Type: {platform.machine()}")
    print(f"Processor: {platform.processor()}")
    print(f"Platform Info: {platform.platform()}")
    uname_info = platform.uname()
    print(f"System: {uname_info.system}")
    print(f"Node Name: {uname_info.node}")
    print(f"Release: {uname_info.release}")
    print(f"Version: {uname_info.version}")
    print(f"Machine: {uname_info.machine}")
    print(f"Processor: {uname_info.processor}")

display_system_info()

4. Summary

The platform module provides a range of functions to access information about the operating system and hardware platform. It is useful for gathering system information, checking compatibility, and debugging. Use the functions in this module to retrieve details about the system where your Python code is running.