/* jquery */ /* jquery accordion style*/ /* jquery init */

28 March 2013

Learn Python - Introducing Modules

Up to now we've just used some basic Python keywords and functions. However, Python has a rich set of functionality. The Standard Python Library contains an extensive collection of modules - well over 200 of them in fact.

To access all this functionality we need to load them using the 'import' keyword. Returning to our previous calculation theme for the moment let's load in the 'math' module, like this:

>>> import math

Now we can access the 'math' module's constants and functions. All we need to do is prefix them with 'math.' to indicate they belong to this module. Here's a few examples of what's possible:

>>> math.sqrt(27)
>>> math.trunc(2.46)
>>> radius = 9
>>> math.pi * (radius*radius)

The first statement finds the square root of 27 with the 'math.sqrt' function. In the second statement we truncate the number 2.46 with the 'math.trunc' function to produce 2. In the third statement we set a variable called 'radius' to be 9. The last statement, as I'm sure many of you already recognise, calculates the area of a circle, multiplying the constant 'math.pi' and the square of the 'radius' value.

Here's another useful module that provides platform information:

>>> import platform
>>> platform.system()
>>> platform.processor()
>>> platform.python_version()

With just one import statement and three module function calls we obtained the Raspberry Pi's operating system, the CPU processor type and the version of the Python language itself.

The last module we'll try, for now anyway, is the operating system 'os' module. It's a particularly useful module which can access the power of Linux. Here are just a few examples of what's possible:

>>> import os
>>> os.getlogin()
>>> os.getcwd()
>>> os.system("ls")
>>> os.system("ps")

After importing the 'os' module we use a function called 'os.getlogin' to display the login user name. Next, the function 'os.getcwd' displays the path of our current working directory, in this case it's the directory in which the terminal window is running. All useful stuff, but the best is yet to come.

It's 'os.system' that's the real star here. With this simple looking function we can run any Linux command. This means we can write a Python program to automate any number of Raspberry Pi operating system commands. We could search for files, start programs, perform backups, monitor running processes, and much more.

Here we have two simple examples. The 'ls' command is like 'dir' command in Windows, and lists the files in the current working directory (the one we displayed with 'os.getcwd'). Try replacing 'ls' with 'ls /' to see all the files in your Raspberry Pi's root directory.

The final statement shows the running processes (or programs) that belong to our session, which will include 'sh' shell scripts and the 'python' process we're using now. Try changing the 'ps' to 'ps -l' and you'll see more detail on each process.

Later in the series we'll explore more modules and the functionality they contain. For a full list of Python's standard modules visit the python.org reference pages.

As you can see, the interactive mode is a fast way to try out Python statements. Try entering this command to discover more about keywords and the language.

>>> help()

But, for now we'll exit the interactive mode with the this command:

>>> quit()

A post from my Learn Python on the Raspberry Pi tutorial.