🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
systemnodereleaseversionmachineprocessorplatformunamearchitecture
- 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.
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment