Jan. 16, 2013

Using the Platform module in Python

What is it used for?

The platform module in Python is used to access the underlying platform’s data,
such as, hardware, operating system, and interpreter version information.

The platform module includes tools to see the platform’s hardware, operating
system, and interpreter version information where the program is running.

How do I use it?

You start with importing the platform module in your program, like this:
import platform

You then specify what you want to find out (more on that below).

For example, if you want to find out which python version you are using, 
simply add python_version() to the platform, like so:
print platform.python_version()  

This will return the Python version as a string. 

On my computer it looks like this:

Platform Functions

Let's have a look at the different platform functions we can use
returns information about the bit architecture 

returns the machine type, e.g. 'i386'.

returns the computer’s network name (may not be fully qualified!)

returns a single string identifying the underlying platform with as much useful
information as possible.

returns the (real) processor name, e.g. 'amdk6'.

returns a tuple (buildno, builddate) stating the Python build number and
date as strings.

returns a string identifying the compiler used for compiling Python.

returns the Python version as string 'major.minor.patchlevel'

returns a string identifying the Python implementation. 
Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’.

returns the system’s release, e.g. '2.2.0' or 'NT'

returns the system/OS name, e.g. 'Linux', 'Windows', or 'Java'.

returns the system’s release version, e.g. '#3 on degas'

returns a tuple of strings (system, node, release, version, machine, processor)
identifying the underlying platform.

Operating System and Hardware Info

Let's show some examples with an output, to see how this works. 

(you can find more examples here)
import platform

print 'uname:', platform.uname()

print 'system   :', platform.system()
print 'node     :', platform.node()
print 'release  :', platform.release()
print 'version  :', platform.version()
print 'machine  :', platform.machine()
print 'processor:', platform.processor()


uname: ('Linux', 'Mwork', '3.5.0-21-generic', '#32-Ubuntu SMP Tue Dec 11 18:51:59 
 UTC 2012', 'x86_64', 'x86_64')

system   : Linux
node     : Mwork
release  : 3.5.0-21-generic
version  : #32-Ubuntu SMP Tue Dec 11 18:51:59 UTC 2012
machine  : x86_64
processor: x86_64

Please see the following URL's for more information


Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.