| by Arround The Web | No comments

Python Platform Module

The platform system information provides details about the operating system that runs your Python code. This information is used to ensure that the code you created will work on different platforms. It also helps with creating cross-platform applications. In Python, the built-in module called “platform” is used to return all the information related to the system. We can also use this module to get information related to Python such as version, build date, compiler info, and others.

This Python article will provide you with a detailed guide on the Python platform module via the following contents:

What is a Python Platform Module?
How to Use a Python Platform Module?

Bonus: Retrieve Information on Various Linux Distros
Conclusion

What is a Python Platform Module?

The “platform” module in Python is an inbuilt module in Python. It is used to provide system information. It retrieves details about the platform on which the program/code is currently being run. Some of the information it retrieved includes operating system, hardware, and interpreter version details.

The “platform” module can be used to verify that the code is suitable with the Python installed on your system. It can also be used to verify that the hardware specifications meet the requirements of the program.

How to Use a Python Platform Module?

The platform module provides several functions that can be used to retrieve information about the system such as “platform.system()”, “platform.machine()”, “platform.processor()”, etc. To use all these functions, we need to import the platform module using the below syntax:

import platform

 
Let’s perform a different example using the platform module functions:

Example 1: Retrieving Platform Processor Information

The “platform.processor()” function of the “platform” module retrieves the information related to the platform processor. Here, in this code, we imported the platform module and called the “platform.processor()” method”:

import platform
print('Retrieving Platform processor:\n', platform.processor())

 
The platform processor information displayed this:


Example 2: Retrieving Platform Information

The “platform.platform()” method retrieves the string including the useful information related to the underlying platform. Type this in Python to retrieve platform information:

import platform
print('Retrieving Platform Information:', platform.platform())

 
The above code displays this:


Example 3: Retrieving System Information

The “platform.uname()” method retrieves the system information in the form of a tuple. The tuple contains information about the system, machine, version, processor, node, and others. This function is efficient as it reduces the calling of individual functions for specific information:

import platform
print('Retrieving System Info:\n')
print(platform.uname())

 
The retrieve tuple containing information is shown below:


Note: To retrieve the system information of different operating systems such as MacOS. You can use the “platform.mac_ver()” method.

Example 4: Retrieving Machine Type

The “platform.machine()” method retrieves information such as the size of the register available in the core related to the machine type. To retrieve machine-type information use this code:

import platform
print('Retrieving Machine Type:', platform.machine())

 
Here is the output:


Example 5: Retrieving Platform Architecture

The “platform.architecture()” method retrieves tuple output containing the information related to the system bit architecture and linkage format. Here the system bit architecture means the number of bits used by the platform processor. To retrieve the platform architecture, utilize the below code:

import platform
print('Retrieving Platform Architecture:', platform.architecture())

 
The following output displays this:


Example 6: Retrieving the Network Name of the System

The “platform.node()” method retrieves the string that shows the details related to nodes such as the system network name. Take this code as an example:

import platform
print('Retrieving Network Name of System: ', platform.node())

 
The following output is displayed to the console:


Example 7: Retrieving Operating System Name

The “platform.system()” method retrieves the operating system name of the device that is currently being used to run the program. To get this, run the following code:

import platform
print('Retrieving Operating System Name: ', platform.system())

 
The operating system name is shown below:


Example 8: Retrieving Python Implementation and Version

The “Implementation” in Python is a way in which Python is executed. Some of the Python implementations are CPython, Jython, IronPython, and others. To check the implementation and the Python version the “platform” module method “platform.python_implementation()” and “platform.python_version()” are used in Python:

import platform
print('Retrieving Python Implementation: ',platform.python_implementation())
print('\nRetrieving Python Version: ', platform.python_version())

 
The below output shows Python implementation and version:


Example 9: Retrieving Python Build Date and Number

The “platform.python_build()” method retrieves the build date and a number of the Python. It retrieves a tuple that contains both values. To get the build date and number of Python, run the below/following code:

import platform
print('Retrieving Python Build Date and Number:\n')
print(platform.python_build())

 
Here is the output:


Example 10: Retrieving Python Compiler Information

The information on the Python compiler that is used to convert the code into low-level language is displayed using the “platform.python_compiler()”  method. For instance, check this code below that retrieved the compiler info of your current Python:

import platform
print('Retrieving Python Compiler Information:\n')
print(platform.python_compiler())

 
The output will look like this:

Bonus: Retrieve Information on Various Linux Distros

We can also retrieve information from various Linux distros such as Ubuntu, Fedora, and others using the platform module of Python. To retrieve the information, you need to enter into a Python shell and execute the following code:

import platform
print(platform.uname())

 
The information related to the Linux operating system is shown below:

Conclusion

The “platform” module in Python retrieves information such as system processor name, machine type, platform information, and others. By using various platform module functions we retrieve the details related to Python and the system currently running Python. We can also retrieve information about other Linux distros with the Python platform module. This guide delivered an in-depth analysis of Python “platform” modules via multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply