Selected Functions From The Platform Module
In this section, we will cover some of the functions offered in platform module in python.
The platform()
Function
The platform module provides access to information about the underlying platform, including hardware, operating system, and interpreter version. There is a function within the module called platform()
that returns a string describing the environment, mainly intended for human reading rather than automated processing. This function takes two optional arguments:
1. aliased: When set to True (or any non-zero value), it may display alternative underlying layer names instead of the common ones.
2. terse: When set to True (or any non-zero value), it may provide a briefer form of the result if possible.
Here's how you can use the platform()
function:
from platform import platform
# Without any arguments
print(platform())
# With aliased set to True
print(platform(aliased=True))
# With terse set to True
print(platform(terse=True))
Using the sample program with different platforms, here are the outputs obtained:
Platform: Intel x86 + Windows Vista (32 bit)
Output: Windows-Vista-6.0.6002-SP2
Platform: Intel x86 + Gentoo Linux (64 bit)
Output: Linux-3.18.62-g6-x86_64-Intel-R-_Core-TM-i3-2330M_CPU@_2.20GHz-with-gentoo-2.3
Platform: Raspberry PI2 + Raspbian Linux (32 bit)
Output: Linux-4.4.0-1-rpi2-armv7l-with-debian-9.0
You can run the provided sample program in IDLE on your local machine to see what output you will get for your platform. Here is what I got when running the code on my machine:-
The machine()
Function
There is a function called machine()
that can be used to find out the generic name of the processor running your operating system along with Python and your code. When you call this function, it returns a string representing the processor's name.
Here are the results of running the sample program (machine()
) on three different platforms:
Intel x86 + Windows ® Vista (32-bit):
Result: "x86Output"
Intel x86 + Gentoo Linux (64-bit):
Result: "x86_64Output"
Raspberry PI2 + Raspbian Linux (32-bit):
Result: "armv7l"
Here is some example:
from platform import machine
print(machine())
By using this function, you can easily determine the processor type on which your Python code is currently running.
The processor()
Function
The processor()
function is a Python code that retrieves and returns the real processor name, if possible. Here, we'll run the sample program on three different platforms and observe the outputs.
from platform import processor
print(processor())
Results on Three Platforms:
Intel x86 + Windows Vista (32-bit):
Output: x86Output
Intel x86 + Gentoo Linux (64-bit):
Output: Intel(R) Core(TM) i3-2330M CPU @ 2.20GHzOutput
Raspberry Pi 2 + Raspbian Linux (32-bit):
Output: armv7lOutput
Feel free to test this program on your local machine to discover your processor name! Here is what I got when running it on my local machine:
The system()
Function
The system()
function allows you to obtain the generic name of the operating system as a string. Example:
from platform import system
print(system())
Sample Outputs:
Intel x86 + Windows ® Vista (32 bit):
OS Name: WindowsOutput
Intel x86 + Gentoo Linux (64 bit):
OS Name: LinuxOutput
Raspberry PI2 + Raspbian Linux (32 bit):
OS Name: Linux
Note: The actual output will depend on the specific operating system running on your device.
The version()
Function
The version()
function provides the operating system version as a string. Example Code:
from platform import version
print(version())
Output:
Intel x86 + Windows ® Vista (32 bit):
Version: 6.0.6002
Intel x86 + Gentoo Linux (64 bit):
Version: #1 SMP PREEMPT Fri Jul 21 22:44:37 CEST 2017
Raspberry PI2 + Raspbian Linux (32 bit):
Version: #1 SMP Debian 4.4.6-1+rpi14 (2016-05-05)
You can run the provided code and check the output to get the OS version for your system.
Python Version Check Functions
Python provides several dedicated functions to check the version of Python running your code. Two commonly used functions are:
1. python_implementation()
:
This function returns a string representing the Python implementation being used. Typically, you will see "CPython" unless you are using a non-canonical Python branch.
2. python_version_tuple()
:
This function returns a three-element tuple containing:
- The major part of Python's version.
- The minor part of Python's version.
- The patch level number of Python's version.
Example Code:
from platform import python_implementation, python_version_tuple
# Check the Python implementation
print(python_implementation())
# Check the Python version components
for component in python_version_tuple():
print(component)
Example Output:
Please note that the actual output of the program may vary depending on your specific Python version.