Python-courses-python-modules

1 - About the platform module

Python provides a built-in module called platform to provide various system information. The Platform module is used to retrieve as much information as possible about the platform on which the program is currently running.This module plays a crucial role when you want to check if your program is compatible with the python version installed on a particular system. or whether the hardware specifications meet your program requirements. This module already exists in the python library and does not require installation using the pip utility. It can be imported using the following syntax:

import platform

2 - Methods associated with the platform module

2.1 - The platform.processor() method

Example: display processor informations

# import of the platform module
import platform
# display of processor information
print ('Processor:', platform.processor ())
# The output is: Processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel

2.2 - platform.platform ()

Returns a unique string identifying the underlying platform with as much useful information as possible.

Example

# import of the platform module
import platform
# display of the machine type
print ('Platform processor:', platform.platform ())
# Displays: Platform processor: Windows-7-6.1.7601-SP1

2.3 The platform.architecture() method

This function returns a tuple that stores information about the binary architecture (number of bits in the platform processor) and the binding format.

Example: display of the architecture of the machine

# import of the platform module
import platform
# display of the platform architecture
print ('Platform architecture:', platform.architecture ())
# Displays: Platform architecture: ('64bit', 'WindowsPE')

2.4 - The platform.machine() method

This function returns a string that displays the machine type, by machine type here, this means information that indicates the width or size of the registers available in the kernel.

Example: display of the machine type

# import of the platform module
import platform
# display of the machine type
print ('Machine type:', platform.machine ())
# Displays: Machine type: AMD64

2.5 - The platform.node() method

This function returns a string that displays information about the node, essentially the name of the machine within the network.

Example: display the name of the network node

# coding: utf-8
# import of the platform module
import platform
# node information
print ('Name of the node within the network:', platform.node ())
# Displays: Name of the node within the network: acer-PC

2.6 - platform.system()

This function returns a string that displays the name of the operating system on the current device used to run the program.

Example: display of the name of the operating system

# coding: utf-8
# import of the platform module
import platform
# operating system name
print ('System:', platform.system ())
# Displays: System: Windows

2.7 - The platform.platform() method

This function returns a single string containing as much useful information as possible to retrieve from the system. The output may differ from system to system.

Example: display information on the platform

# import of the platform module
import platform
# display information on the platform
print ('Platform information:', platform.platform ())
# Displays: Platform Info :: Windows-7-6.1.7601-SP1

2.8 - The platform.python_build() method

This function returns a tuple which stores information about python build date and build number. This information is stored in the tuple as a string data type.

Example: Display python version.

# coding: utf-8
# import of the platform module
import platform
# display the python build number
print ('Python build number and date:', platform.python_build())
# Displays: Python build number and date: ('tags / v3.7.2: 9a3ffc0492', 'Dec 23 2018 23:09:28')

2.9 - The platform.uname() method

Quite portable uname interface. Returns a tuple of strings (system, node, version, version, machine, processor) identifying the underlying platform. Note that unlike the os.uname() function, this also returns possible processor information as an additional tuple input.

Example

# import of the platform module
import platform
print ('Platform info:', platform.uname())
# The output is:
#Plate forme info: uname_result(system='Windows', node='acer-PC', release='7', version='6.1.7601', machine='AMD64', processor='Intel64 Family 6 Model 58 Stepping 9,GenuineIntel')

2.10 - platform.python_version ()

This function returns a string that displays the version of Python currently running on the system.

Example: Display of the python version

# Python program to display the python version
# import of the platform module
import platform
# display the python version
print ('Python version:', platform.python_version ())
# Displays: Python version: 3.7.2

2.11 - platform.python_compiler()

This method returns a string identifying the compiler used to compile Python.

Example: Displaying information from the python compiler

# import of the platform module
import platform
# display of the python compiler
print ('Python compiler:', platform.python_compiler ())
# Displays: Python compiler: MSC v.1916 64 bit (AMD64)

2.12 - The platform.python_branch() method

This function returns a string displaying information about the python SCM branch, SCM stands for Source Code Manager, it is a tool used by programmers to manage source code. SCM is used to track software revisions.

Example: Display of python SCM information

# Python program to display python SCM information
# import of the platform module
import platform
# display of python SCM information
print ('Python MSC:', platform.python_compiler ())
# Displays: Python SCM: MSC v.1916 64 bit (AMD64)

2.13 - The platform.python_implementation() method

This function returns a string that displays information about the python implementation. The possible outputs of this function are CPython, JPython, PyPy, IronPython.

Example: Viewing the python implementation

# coding: utf-8
# Python program to show the python implementation
# import of the platform module
import platform
# display the python implementation
print ('Python implementation:', platform.python_implementation ())
# Displays: Python implementation: CPython
Younes Derfoufi
my-courses.net

Leave a Reply