A quick dive into Python's platform module

A quick dive into Python's platform module

The platform module in Python is designed to give you specific information about the underlying platform. In computing, a platform is the environment a piece of software is running on, typically the hardware and operating system of the computer you're working with.

If you have a program that relies on certain hardware specs that differ across machines (e.g., the type of processor), the platform module can help you identify these details so that you can adjust your code accordingly.

Let’s run through some functions and find out what useful information they can tell us about our computer.

To use platform, all you have to do is import it (assuming you have Python installed). Just do import platform . Once imported, the module becomes available on your platform (pun intended) for you to explore.

Now, to the functions:

  • platform.platform() returns information about your platform.

platform.architecture() returns details about the bit size of the Python Interpreter (32-bit or 64-bit) and the file format for executable files. For instance, when I ran this command on my Windows 10 laptop, I got the following output:

>>> platform.architecture()
('64bit', 'WindowsPE')
>>>

The “64bit” part tells me that my computer is running a 64-bit operating system, while “WindowsPE,” which stands for “Windows Preinstallation Environment,” refers to the tiny operating system that installs or repairs the main Windows OS.

  • platform.machine() displays information about the type of machine or processor Python is running on (e.g., AMD64).

  • platform.node(). This function reveals the name assigned to your device on the network (e.g., mine is DESKTOP-CG23O44). Think of this as your computer’s secret identity within the local area network of interconnected computers.

  • platform.processor() displays your processor’s real name in full. Running this command on my computer returns the following:

>>> platform.processor()
'Intel64 Family 6 Model 78 Stepping 3, GenuineIntel'
>>>
  • platform.system() returns the name of your operating system (e.g., Linux).

  • platform.uname() is like a shorthand for .system(), .node(), and .machine() , meaning that it returns information about the name of your system, its version number, node, and some other details.

Some platform functions tell you something about your Python Interpreter. Examples:

  • platform.python_version() returns information about the version of Python you're using.

  • platform.python_build() displays the Python build date and build number.

platform.python_compiler() tells you the compiler used for Python.

That's all for this intro. Check out the documentation to learn about platform-specific functions, such as platform.win32_edition() for Windows and platform.java_ver() for Java.