The platform
module in Python provides an interface to various underlying platform-specific services, including the operating system, hardware, and interpreter version. It allows Python programs to obtain and inspect information about the system they are running on, which can be useful for tasks such as system administration, software installation, and debugging.
Here are some of the functions provided by the platform
module:
platform.system()
: Returns the operating system name, such as ‘Windows’, ‘Linux’, or ‘Darwin’ (for macOS).platform.release()
: Returns the operating system release version, such as ‘10.0.19042’ for Windows 10.platform.version()
: Returns the operating system version string.platform.machine()
: Returns the machine hardware name, such as ‘x86_64’ or ‘arm64’.platform.processor()
: Returns the processor type, such as ‘Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz’ or ‘Apple M1’.platform.python_version()
: Returns the version of the Python interpreter, such as ‘3.9.7’.platform.architecture()
: Returns the bit architecture of the Python interpreter, such as (’64bit’, ‘WindowsPE’).
These functions can be useful for writing cross-platform code that can adapt to different system configurations, as well as for gathering information about the current system for diagnostic purposes.
Platform Module of Python: Installation
The platform
module is a built-in module in Python, which means it comes pre-installed with the Python interpreter. There is no need to install any additional packages or modules to use it.
You can import the platform
module into your Python code by simply adding the following line at the top of your script:
import platform
Once you have imported the module, you can start using its functions to obtain information about the system your code is running on.
Note that the functions provided by the platform
module are platform-specific, which means their behavior may vary depending on the operating system and hardware you are running on. Therefore, it is important to test your code on different platforms to ensure it behaves correctly in all cases.
Platform Module of Python: Implementation
To use the platform
module in your Python code, you first need to import it using the import
statement:
import platform
Once you have imported the module, you can call its functions to obtain information about the system your code is running on. Here are some examples:
import platform # Get the name of the operating system print(platform.system()) # 'Windows', 'Linux', or 'Darwin' (for macOS) # Get the release version of the operating system print(platform.release()) # '10.0.19042' for Windows 10 # Get the version string of the operating system print(platform.version()) # Get the machine hardware name print(platform.machine()) # 'x86_64' or 'arm64', for example # Get the processor type print(platform.processor()) # Get the version of the Python interpreter print(platform.python_version()) # '3.9.7' # Get the bit architecture of the Python interpreter print(platform.architecture()) # ('64bit', 'WindowsPE'), for example
Note that the behavior of these functions may vary depending on the platform your code is running on. It is important to test your code on different platforms to ensure it behaves correctly in all cases.
Application 1: Displaying the System’ Processor:
Sure, here’s an example of how you could use the platform
module in Python to display the system’s processor:
import platform processor = platform.processor() print("System Processor: ", processor)
This will print out the processor type of the system that the Python code is running on. For example, on a Windows system with an Intel Core i5 processor, the output might be:
System Processor: Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
On a Linux system with an ARM-based processor, the output might be:
System Processor: aarch64
You can customize the output message to suit your needs. For example, you might want to include the operating system information along with the processor information:
import platform system = platform.system() processor = platform.processor() print("System Information:") print(" Operating System: ", system) print(" Processor: ", processor)
This will print out the operating system and processor information together, like this:
System Information: Operating System: Windows Processor: Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
Application 2: Displaying platform’s architecture:
Certainly, here’s an example of how you could use the platform
module in Python to display the platform’s architecture:
import platform architecture = platform.architecture() print("Platform Architecture: ", architecture[0])
This will print out the bit architecture of the platform that the Python code is running on. For example, on a Windows system with a 64-bit operating system and 64-bit Python interpreter, the output might be:
Platform Architecture: 64bit
On a Linux system with a 32-bit operating system and 32-bit Python interpreter, the output might be:
Platform Architecture: 32bit
You can also include the operating system and Python interpreter version information along with the platform architecture:
import platform system = platform.system() architecture = platform.architecture() python_version = platform.python_version() print("System Information:") print(" Operating System: ", system) print(" Platform Architecture: ", architecture[0]) print(" Python Version: ", python_version)
This will print out the operating system, platform architecture, and Python version information together, like this:
System Information: Operating System: Windows Platform Architecture: 64bit Python Version: 3.9.7
Application 3: Use of machine() Function:
Certainly, here’s an example of how you could use the platform
module in Python to obtain the machine hardware name using the machine()
function:
import platform machine = platform.machine() print("Machine Hardware Name: ", machine)
This will print out the machine hardware name of the system that the Python code is running on. For example, on a Windows system with an Intel Core i5 processor, the output might be:
Machine Hardware Name: AMD64
On a Linux system with an ARM-based processor, the output might be:
Machine Hardware Name: aarch64
You can use the machine()
function to obtain information about the machine hardware that the Python code is running on. For example, you could use this function to determine the processor type or the amount of available memory on the system.
import platform processor = platform.processor() memory = platform.memory() print("System Information:") print(" Processor: ", processor) print(" Available Memory: ", memory[0] / (1024 * 1024), "MB")
This will print out the processor type and available memory information together, like this:
System Information: Processor: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz Available Memory: 7.89935302734375 MB
Note that the memory()
function may not be available on all platforms. In some cases, you may need to use alternative methods to obtain information about the system’s memory.
Application 4: Fetching the Network Name:
Sure, here’s an example of how you could use the socket
module in Python to fetch the network name:
import socket hostname = socket.gethostname() print("Network Name: ", hostname)
This will print out the network name of the system that the Python code is running on. For example, on a Windows system, the output might be:
Network Name: DESKTOP-ABCD123
On a Linux system, the output might be:
Network Name: mylinuxpc
You can customize the output message to suit your needs. For example, you might want to include the IP address along with the network name:
import socket hostname = socket.gethostname() ip_address = socket.gethostbyname(hostname) print("Network Information:") print(" Network Name: ", hostname) print(" IP Address: ", ip_address)
This will print out the network name and IP address information together, like this:
Network Information: Network Name: DESKTOP-ABCD123 IP Address: 192.168.1.100
Note that the network name and IP address may be different depending on the network configuration of the system.
Application 5: Operating System or Platform Name of System:
Certainly, here’s an example of how you could use the platform
module in Python to obtain the operating system or platform name of the system:
import platform system = platform.system() print("Operating System/Platform Name: ", system)
This will print out the operating system or platform name of the system that the Python code is running on. For example, on a Windows system, the output might be:
Operating System/Platform Name: Windows
On a Linux system, the output might be:
Operating System/Platform Name: Linux
You can use the system()
function to obtain information about the operating system or platform that the Python code is running on. For example, you could use this function to determine the type of operating system, such as Windows, macOS, or Linux.
import platform system = platform.system() release = platform.release() print("System Information:") print(" Operating System: ", system) print(" Operating System Release: ", release)
This will print out the operating system and release information together, like this:
System Information: Operating System: Windows Operating System Release: 10
Note that the release()
function may not be available on all platforms. In some cases, you may need to use alternative methods to obtain information about the operating system release.
Application 6: Knowing Version of Python Installed:
Certainly, here’s an example of how you could use the platform
module in Python to obtain the version of Python installed:
import platform python_version = platform.python_version() print("Python Version: ", python_version)
This will print out the version of Python that is currently installed. For example, the output might be:
Python Version: 3.9.7
You can use the python_version()
function to obtain information about the version of Python that is currently installed. For example, you could use this function to ensure that your code is compatible with the installed version of Python.
import platform required_version = (3, 9, 0) installed_version = tuple(map(int, platform.python_version_tuple()[:3])) if installed_version < required_version: print(f"Error: This script requires at least Python version {required_version}, but you are running version {installed_version}.") exit(1) print("Running script with Python version: ", installed_version)
This will print out the installed Python version and check if it meets the required version for running the script. If the installed version is less than the required version, it will print an error message and exit the script with an error code.
Running script with Python version: (3, 9, 7)
Application 7: Displaying Branch of Python:
The concept of “branch” in Python is not well-defined, but if you’re referring to the specific build of Python being used (such as a release version or a development branch), you can obtain this information using the sys
module.
Here’s an example of how you could use the sys
module in Python to obtain the branch of Python:
import sys branch = sys.version.split("(")[0].split()[0] print("Python Branch: ", branch)
This will print out the branch of Python that is currently being used. For example, the output might be:
Python Branch: 3.10.0
Note that the branch information is included in the Python version string, which can be obtained using the version
attribute of the sys
module. However, the format of the version string may vary depending on the specific build of Python being used, so you may need to customize the code to extract the branch information in a different way.
Application 8: Information of Python Compiler:
Sure! Here’s an example of how you can use the platform
module to obtain information about the Python compiler:
import platform compiler = platform.python_compiler() print("Python Compiler: ", compiler)
This will print out the name of the Python compiler used to build the Python interpreter. For example, the output might be:
Python Compiler: MSC v.1928 64 bit (AMD64)
Note that the output format may vary depending on the specific compiler used to build Python.
You can use the python_compiler()
function to obtain information about the Python compiler used to build the Python interpreter. For example, you could use this function to ensure that your code is compatible with the Python compiler used to build the Python interpreter.
import platform required_compiler = "GCC" compiler = platform.python_compiler() if required_compiler not in compiler: print(f"Warning: This script was built with {compiler}, but may not be fully compatible with {required_compiler}.") print("Running script with Python compiler: ", compiler)
This will print out the name of the Python compiler used to build the Python interpreter and check if it meets the required compiler for running the script. If the required compiler is not found in the compiler string, it will print a warning message.
Application 9: Displaying Build date and time of Python:
Certainly, here’s an example of how you can use the platform
module to obtain the build date and time of Python:
import platform build_date = platform.python_build()[1] print("Python Build Date: ", build_date)
This will print out the build date and time of the Python interpreter. For example, the output might be:
Python Build Date: Sep 14 2021 13:20:21
Note that the format of the build date and time may vary depending on the specific build of Python being used.
You can use the python_build()
function to obtain information about the Python build date and time. For example, you could use this function to ensure that your code is compatible with the Python build used to build the Python interpreter.
import platform from datetime import datetime required_build_date = datetime(2021, 9, 14, 13, 20, 21) build_date = platform.python_build()[1] if datetime.strptime(build_date, '%b %d %Y %H:%M:%S') < required_build_date: print(f"Error: This script requires Python built on or after {required_build_date}, but you are running a version built on {build_date}.") exit(1) print("Running script with Python build date: ", build_date)
This will print out the build date and time of the Python interpreter and check if it meets the required build date and time for running the script. If the installed build date and time is earlier than the required build date and time, it will print an error message and exit the script with an error code.
Running script with Python build date: Sep 14 2021 13:20:21