Understanding the Computer Environment Layers and Utilizing Python's Platform Module

Imagine your computer's environment as a 3D cube with different layers or platforms. Each layer plays a crucial role in running your program and performing various tasks.

layers or platforms

Top Layer: Your Code
Your program's code is located at the top layer.

Python Runtime Environment Layer:
Just below your code is Python's runtime environment, which supports and executes your program.

Operating System (OS) Layer:
The next layer contains the operating system (OS), which provides various services to Python. Python relies on the OS to perform tasks like file processing and communication with physical devices.

Bottom Layer: Hardware
At the bottom-most layer lies the hardware, including the processor(s), network interfaces, and human interface devices like mice and keyboards. This hardware is essential for the computer to function properly.

How Actions are Performed

  • When your code needs to perform an action, such as creating a file, it goes through the layers.
  • Your code sends a request to Python.
  • Python processes the request to meet the OS requirements and passes it down.
  • The OS checks the request's validity and performs the necessary operations.
  • The hardware is activated to satisfy the OS's needs, like writing data to storage devices.

Usually, you don't need to be aware of all these intricate processes. You just want your file to be created smoothly.

However, sometimes you might want to know more about your computer's setup, such as the OS's name or details about the hardware.

To access such information, there's a module called platform in Python. This module provides functions that allow you to retrieve information about your environment, such as the operating system and hardware characteristics. Let's explore some of these functions.

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:-

OUTPUT:

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())
OUTPUT:

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:

OUTPUT:

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:

OUTPUT:

Please note that the actual output of the program may vary depending on your specific Python version.

Python Module Index

Python modules form their own vast universe, with Python itself being just one galaxy. Exploring the depths of these modules can take more time than getting familiar with "pure" Python.

Beyond the basics, there are hundreds of additional modules created and maintained by the global Python community. These modules cater to niche applications like genetics, psychology, and astrology. They are not distributed with Python or through official channels, making the Python universe almost infinite.

Don't worry, you don't need to learn all these modules since many are very specific. Instead, you can focus on finding the ones you need and learning how to use them, which is relatively easy.

For a comprehensive list of standard Python modules, refer to this link: https://docs.python.org/3/py-modindex.html.

In the upcoming section, we'll guide you on how to write your own module.

End Of Article

End Of Article