Python platform module – Quick Introduction

Platform Module Title

Python has a Platform Module that contains functions for dealing with the platform the code is being run on. In this tutorial, we will discuss the module and look at most of its useful functions.

About the Platform Module

The platform module is used to retrieve information about the system or platform. We can use this module to perform compatibility checks. When we have a Python program that requires certain conditions to be met, e.g., the architecture of the processor, the operating system in use, or the Python version the system has, then this module can be used.

These specifications are used to determine how well the Python code will run on the system.

Not only for compatibility checks, but the module can also be used for the sake of it. We have many programs that tell us our platform specifications and any such program written in Python can use this module.

The module is named “platform”, so to import it without an alias we can do this:

import platform

Functions offered by the platform module

Now let us begin with the available functions. For each function, the examples are run on a Linux virtual machine.

Read also – Python os module

1. Platform Architecture

Returns a tuple containing the bit architecture (number of bits in the processor bus) and the linkage format of the processor used by the platform. Both values are returned as strings.

platform.architecture()
('64bit', 'ELF')

2. Machine Type

Returns a string containing the machine type (size of registers used in the processor) of the platform.

platform.machine()
'x86_64'

3. Network Name

Returns a string containing the platform’s network name (the name displayed for the platform if it is in a network).

platform.node()
'sumeet-VirtualBox'

4. Platform Information

Returns a single string containing useful information about the underlying platform. The function retrieves as much information as possible and then returns a string that is human-readable, so it may look different for different platforms.

platform.platform()
'Linux-5.4.0-58-generic-x86_64-with-glibc2.29'

5. Processor Name

Returns a single string containing the actual name of the processor used by the platform.

platform.processor()
'Intel64 Family 6 Model 158 Stepping 10, GenuineIntel'

6. Python Build

Returns a tuple containing the build number and the build date of the Python installation on the platform. Both values in the tuple are strings.

platform.python_build()
('default', 'Jan 27 2021 15:41:15')

7. Python Compiler

Returns a string containing the name of the compiler used for compiling Python on the platform.

platform.python_compiler()
'GCC 9.3.0'

8. Python Implementation

Returns a string containing the information about the implementation of Python installed on the platform.

platform.python_implementation()
'CPython'

9. Python Version

Returns a string identifying the version of Python installed on the platform.

The string is of the format “major.minor.patchlevel“.

platform.python_version()
'3.8.5'

10. Python Version Tuple

Returns the version of Python installed on the platform as a tuple.

The tuple is of the format “(major, minor, patchlevel)“.

platform.python_version_tuple()
('3', '8', '5')

11. OS Release

Returns the release information of the operating system as a string.

platform.release()
'5.4.0-58-generic'

12. OS Name

Returns the name of the operating system on the platform as a string.

platform.system()
'Linux'

13. OS Release Version

Returns the release version of the operating system on the platform as a string.

platform.version()
'#64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020'

14. Platform Information Tuple

Returns a named tuple with six attributes: system, node, release, version, machine, and processor. All these attributes have individual functions, so this function can be used to get all the information we get from the other functions.

platform.uname()
uname_result(system='Linux', node='sumeet-VirtualBox', release='5.4.0-58-generic', version='#64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020', machine='x86_64', processor='Intel64 Family 6 Model 158 Stepping 10, GenuineIntel')

Conclusion

In this tutorial, we studied the platform module in python. We discussed many of its important functions and saw their output.

I hope you had a great time learning and see you in the next tutorial.