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

Platform Functions

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

platform.machine()
returns the machine type, e.g. 'i386'.

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

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

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

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

platform.python_compiler()
returns a string identifying the compiler used for compiling Python.

platform.python_version()
returns the Python version as string 'major.minor.patchlevel'

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

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

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

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

platform.uname() 
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
print 'system   :', platform.system()
print 'node     :', platform.node()
print 'release  :', platform.release()
print 'version  :', platform.version()
print 'machine  :', platform.machine()
print 'processor:', platform.processor()

Output

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

Sources
Please see the following URL's for more information

http://docs.python.org/2/library/platform.html
http://www.doughellmann.com/PyMOTW/platform/

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.