Updated On : Mar-01,2021 Tags platform-details
platform - Guide to Retrieve Underlying System's Details using Python

platform - Guide to Retrieve Underlying System's Details using Python

Developers many times need to determine information about the underlying system on which software is running like machine type, OS version, architecture, processor, python build details, etc. This kind of information can be used to make decisions about software configuration based on the underlying system. Python provides us with a module named platform which has a list of methods that provides information about the underlying system. As a part of this tutorial, we'll explain methods provided by platform module whose knowledge can be useful to developers.

We'll start by importing the platform module.

In [1]:
import platform

Determine System Architecture


Important Methods of platform Module

  • architecture(executable=sys.executable,bits='',linkage='') - This method uses an executable give as executable parameter and determines system architecture based on that. The sys.executable returns a path to executable python interpreter which will be used to determine system architecture details. This method returns a tuple (bits, linkage).
    • We can give value to parameters bits and linkage which will be returned if this method is not able to retrieve that details.

In [6]:
bits, linkage = platform.architecture(bits="", linkage="")

print("Processor Type : {}".format(bits if bits else "Not Found"))
print("Linkage        : {}".format(linkage if linkage else "Not Found"))
Processor Type : 64bit
Linkage        : Not Found

Determine Machine Type


Important Methods of platform Module

  • machine() - This method returns the machine type.

In [7]:
machine = platform.machine()

print("Machine : {}".format(machine))
Machine : x86_64

Determine Node Name of the System in Network


Important Methods of platform Module

  • node() - This method returns system's network name.

In [7]:
node_name = platform.node()

print("Node Name of Computer in Network : {}".format(node_name))
Node Name of Computer in Network : sunny-Vostro-3470

Determine Platform Details


Important Methods of platform Module

  • platform(aliased=0,terse=0) - This method returns a string representing underlying platform in a human readable form.
    • If aliased parameter is set to True then returns alias name of the system.
    • If terse parameter is set to True then it returns a string with the minimum information needed to identify the platform.

In [13]:
platform_details = platform.platform()

print("Platform Details : {}".format(platform_details))

platform_details = platform.platform(terse=True)

print("Platform Details : {}".format(platform_details))
Platform Details : Linux-5.4.0-65-generic-x86_64-with-debian-buster-sid
Platform Details : Linux-5.4.0-65-generic-x86_64-with-glibc2.10

Determine Processor Details


Important Methods of platform Module

  • processor() - This method returns a string representing actual processor name.

In [9]:
processor = platform.processor()

print("Processor Details : {}".format(processor))
Processor Details : x86_64

Determine Python Build Details


Important Methods of platform Module

  • python_build() - This method returns a tuple of 2 values where the first value represents the python build number and the second string represents the build date of python.

If you are interested in learning about Python's configuration details then please feel free to check out the tutorial on Python module sysconfig which provides methods for it.

In [12]:
build_no, build_date = platform.python_build()

print("Python Build : Build No - {}, Build Date : {}".format(build_no, build_date))
Python Build : Build No - default, Build Date : Mar 27 2019 22:11:17

Determine Python Compiler Name


Important Methods of platform Module

  • python_compiler() - This method returns a string representing python compiler.

In [13]:
compiler = platform.python_compiler()

print("Python Compiler : {}".format(compiler))
Python Compiler : GCC 7.3.0

Determine Python Implementation Details


Important Methods of platform Module

  • python_implementation() - This method returns python implementation. Ex - Cpython, pypy, jython, etc.

In [14]:
python_implementation = platform.python_implementation()

print("Python Implementation : {}".format(python_implementation))
Python Implementation : CPython

Determine Python Revision Details


Important Methods of platform Module

  • python_revision() - This method returns a string representing Python implementation SCM revision.

In [16]:
python_revision = platform.python_revision()

print("Python Revision : {}".format(python_revision if python_revision else "NA"))
Python Revision : NA

Determine Python Version Details


Important Methods of platform Module

  • python_version() - This method returns a string representing Python version.
  • python_version_tuple() - This method returns a tuple of 3 integer values where first value represents major, second value represents minor and third value represents patch version details.

In [18]:
python_version = platform.python_version()
major,minor,patch = platform.python_version_tuple()

print("Python Version : {}".format(python_version))

print("Python Version : Major-{},Minor-{},Patch-{}".format(major,minor,patch))
Python Version : 3.7.3
Python Version : Major-3,Minor-7,Patch-3

Determine Underlying System's Release Details


Important Methods of platform Module

  • release() - This method returns a string representing system's release. Ex - '2.3.0', '5.1.0'

In [19]:
release_info = platform.release()

print("System Release Information : {}".format(release_info))
System Release Information : 5.4.0-65-generic

Determine Underlying System's OS Name


Important Methods of platform Module

  • system() - This method returns a string representing the underlying OS name. Ex - Linux, Windows, Mac, etc

In [20]:
system = platform.system()

print("System : {}".format(system))
System : Linux

Determine System's Version Details


Important Methods of platform Module

  • version() - This method returns a string representing system version details.

In [25]:
sys_version = platform.version()

print("System Version : {}".format(sys_version))
System Version : #73~18.04.1-Ubuntu SMP Tue Jan 19 09:02:24 UTC 2021

Determine All System Details


Important Methods of platform Module

  • uname() - This method returns a named tuple with six values which are mentioned below.
    • The system attribute represents the OS name.
    • The node attribute represents the system name in the network.
    • The release attribute represents system release details.
    • The version attribute represents system version details.
    • The machine attribute represents the machine name.
    • The processor attribute represents the processor name of the system.

In [30]:
system_details = platform.uname()

print("System Details Instance Type : {}".format(type(system_details)))

print("System            : {}".format(system_details.system))
print("Node Name         : {}".format(system_details.node))
print("Release Details   : {}".format(system_details.release))
print("Version Details   : {}".format(system_details.version))
print("Machine Details   : {}".format(system_details.machine))
print("Processor Details : {}".format(system_details.processor))
System Details Instance Type : <class 'platform.uname_result'>
System            : Linux
Node Name         : sunny-Vostro-3470
Release Details   : 5.4.0-65-generic
Version Details   : #73~18.04.1-Ubuntu SMP Tue Jan 19 09:02:24 UTC 2021
Machine Details   : x86_64
Processor Details : x86_64

Determine System Alias


Important Methods of platform Module

  • system_alias() - This method returns a string that is an alias to the common marketing name of the system.

In [24]:
sys_alias = platform.system_alias(platform.system(), platform.release(), platform.version())

print("System Alias : {}".format(sys_alias))
System Alias : ('Linux', '5.4.0-65-generic', '#73~18.04.1-Ubuntu SMP Tue Jan 19 09:02:24 UTC 2021')

Determine Libc (C Standard Libraries) Version


Important Methods of platform Module

  • libc_ver() - This method returns a string representing a standard C libraries.

In [33]:
c_name, c_ver = platform.libc_ver()

print("GCC Details : {}-{}".format(c_name,c_ver))
GCC Details : glibc-2.10

Determine Version Details of Java, Windows and Mac OS Systems

Please make a NOTE that the below-mentioned methods will work only on systems that are mentioned in their names.


Important Methods of platform Module

  • java_ver() - This method returns a tuple of 4 values (release, vendor_name, vm_info, os_info).
    • The vm_info is again a tuple of (vm_name, csd, ptype).
    • The os_info is again a tuple of (os_name, os_version, os_architecture).
  • mac_ver() - This method returns a tuple of 3 values (release, version_info, machine_details).
    • The version_info is again a tuple of (version, dev_stage, non_release_version).
  • win32_ver() - This method returns a tuple of 4 values (release, vendor_name, csd, ptype).
  • win32_edition() - This method returns a string representing windows addition details.Ex - Enterprise, ServerStandard, etc.
  • win32_is_iot() - This method returns a boolean value True if windows edition returned by win32_edition() method is IoT edition.

This ends our small tutorial explaining methods of platform module. Please feel free to let us know your views in the comments section.



Sunny Solanki  Sunny Solanki