The platform
module in Python provides a way to access underlying platform data, such as the operating system, hardware, and interpreter version. It is useful for writing code that is portable across different platforms and for gathering system information.
Table of Contents
- Introduction
- Key Functions
system
node
release
version
machine
processor
platform
uname
architecture
- Examples
- Getting System Information
- Checking Platform Details
- Real-World Use Case
- Conclusion
- References
Introduction
The platform
module provides functions to access information about the underlying platform on which the Python interpreter is running. This includes details about the operating system, hardware, and Python version.
Key Functions
system
Returns the name of the operating system dependent module imported.
import platform
os_system = platform.system()
print(f'Operating System: {os_system}')
node
Returns the computer’s network name (hostname).
import platform
hostname = platform.node()
print(f'Hostname: {hostname}')
release
Returns the system’s release.
import platform
os_release = platform.release()
print(f'OS Release: {os_release}')
version
Returns the system’s version.
import platform
os_version = platform.version()
print(f'OS Version: {os_version}')
machine
Returns the machine type.
import platform
machine_type = platform.machine()
print(f'Machine Type: {machine_type}')
processor
Returns the (real) processor name.
import platform
processor_name = platform.processor()
print(f'Processor: {processor_name}')
platform
Returns a single string identifying the underlying platform with as much useful information as possible.
import platform
platform_info = platform.platform()
print(f'Platform: {platform_info}')
uname
Returns a named tuple containing six attributes: system
, node
, release
, version
, machine
, and processor
.
import platform
uname_info = platform.uname()
print(f'Uname: {uname_info}')
architecture
Returns the bit architecture and linkage format of the Python interpreter.
import platform
architecture_info = platform.architecture()
print(f'Architecture: {architecture_info}')
Examples
Getting System Information
import platform
print(f'Operating System: {platform.system()}')
print(f'Hostname: {platform.node()}')
print(f'OS Release: {platform.release()}')
print(f'OS Version: {platform.version()}')
print(f'Machine Type: {platform.machine()}')
print(f'Processor: {platform.processor()}')
print(f'Platform: {platform.platform()}')
print(f'Architecture: {platform.architecture()}')
Output:
Operating System: Linux
Hostname: myhostname
OS Release: 5.8.0-50-generic
OS Version: #56-Ubuntu SMP Tue Apr 13 19:36:31 UTC 2021
Machine Type: x86_64
Processor: x86_64
Platform: Linux-5.8.0-50-generic-x86_64-with-glibc2.29
Architecture: ('64bit', 'ELF')
Checking Platform Details
import platform
def check_platform():
if platform.system() == 'Windows':
print('This is a Windows system.')
elif platform.system() == 'Linux':
print('This is a Linux system.')
elif platform.system() == 'Darwin':
print('This is a macOS system.')
else:
print('Unknown system.')
check_platform()
Output (example on Linux):
This is a Linux system.
Real-World Use Case
Custom Installation Script
You can use the platform
module to create custom installation scripts that behave differently based on the underlying platform.
import platform
import subprocess
def install_dependencies():
system = platform.system()
if system == 'Windows':
subprocess.run(['powershell', '-Command', 'Install-WindowsFeature', '-Name', 'Web-Server'])
elif system == 'Linux':
subprocess.run(['sudo', 'apt-get', 'install', '-y', 'nginx'])
elif system == 'Darwin':
subprocess.run(['brew', 'install', 'nginx'])
else:
print('Unsupported system.')
install_dependencies()
Output:
The script will execute the appropriate command to install NGINX based on the detected platform.
Conclusion
The platform
module in Python is used for accessing information about the underlying system. It allows you to write platform-independent code by providing detailed information about the operating system, hardware, and Python interpreter.
Comments
Post a Comment
Leave Comment