Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Use Platform and Keyword Module in Python - DesignLinux

Nov 03 2020
designlinux 0 Comments

The platform module provides an API to get information about the underlying system/platform where our code runs. Information such as OS name, Python Version, Architecture, Hardware information, etc. is exposed via platform module functions. This module does not require installation since it is part of the default libraries that comes with python installation.

First, let’s Import the “platform” module.

# python3
>>> import platform
>>> print("Imported Platform module version: ", platform.__version__)
Import Python Platform Module
Import Python Platform Module

Let’s grab some information about python first, like what is the version, build information, etc..

  • python_version() – Returns the python version.
  • python_version_tuple() – Returns python version in tuple.
  • python_build() – Returns build number and date in the form of a tuple.
  • python_compiler() – Compiler used to compile python.
  • python_implementation() – Returns python implementation like “PyPy”,”CPython”, etc..
>>> print("Python version: ",platform.python_version())
>>> print("Python version in tuple: ",platform.python_version_tuple())
>>> print("Build info: ",platform.python_build())
>>> print("Compiler info: ",platform.python_compiler())
>>> print("Implementation: ",platform.python_implementation())
Check Python Information
Check Python Information

Now let’s grab some system-related information, like OS flavor, release version, processor, etc..

  • system() – Returns system/OS name like “Linux”, “Windows”, “Java”.
  • version() – Returns system version information.
  • release() – Returns the system release version.
  • machine() – Returns machine type.
  • processor() – Returns system processor name.
  • node() – Returns the system network name.
  • platform() – Returns as much as useful information about the system.
>>> print("Running OS Flavour: ",platform.system())
>>> print("OS Version: ",platform.version())
>>> print("OS Release: ",platform.release())
>>> print("Machine Type: ",platform.machine())
>>> print("Processor: ",platform.processor())
>>> print("Network Name: ",platform.node())
>>> print("Linux Kernel Version: ",platform.platform())
Check Linux System Information
Check Linux System Information

Instead of accessing all the system-related information through separate functions, we can use the uname() function which returns a named tuple with all the information like System Name, release, Version, machine, processor, node. We can use index values to access specific information.

>>> print("Uname function: ",platform.uname())
>>> print("\nSystem Information: ",platform.uname()[0])
>>> print("\nNetwork Name: ",platform.uname()[1])
>>> print("\nOS Release: ",platform.uname()[2])
>>> print("\nOS Version: ",platform.uname()[3])
>>> print("\nMachine Type: ",platform.uname()[4])
>>> print("\nMachine Processor: ",platform.uname()[5])

Think of a use case where you want to run your program only in a certain version of python or only in a specific OS flavor, In that case, the platform module is very handy.

Below is a sample pseudocode to check the python version and OS flavor.

import platform
import sys

if platform.python_version_tuple()[0] == 3:
    < Block of code >
else:
    sys.exit()

if platform.uname()[0].lower() == "linux":
    < Block of Code >
else:
    sys.exit()

Python Keyword Module

Every programming language comes with built-in keywords that servers different functionality. For eg: True, False, if, for, etc.. Similarly, python has built-in keywords that cannot be used as identifiers to variable, functions, or class.

The keyword module provides 2 functionality.

  • kwlist – Prints out the list of built-in keywords.
  • iskeyword(s) – Returns true if s is a python defined keyword.
Python Keyword Module
Python Keyword Module

Now that we have come to the end of the article, so far we have discussed 2 python modules (Platform and Keyword). The platform module is very useful when we want to grab some information about the system we are working with. On the other hand, the keyword module provides a list of built-in keywords and functions to check if a given identifier is a keyword or not.

Related

Tags: Python Programming, Python Tips

Install Odoo 14 on CentOS 8

Prev Post

BpyTop – Resource Monitoring Tool for Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 515
  • 1,055,287

DesignLinux.com © All rights reserved

Go to mobile version