Page 58 - Programming the Raspberry Pi Getting Started with Python
P. 58
In this case, the asterisk (*) means “everything.”
Useful Python Libraries
So far we have used the random module, but other modules are included in Python. These modules are
often called Python’s standard library. There are too many of these modules to list in full. However,
you can always find a complete list of Python modules at
http://docs.python.org/release/3.1.5/library/index.html. Here are some of the most useful modules you
should take a look at:
• string String utilities
• datetime For manipulating dates and times
• math Math functions (sin, cos, and so on)
• pickle For saving and restoring data structures on file (see Chapter 6)
• urllib.request For reading web pages (see Chapter 6)
• tkinter For creating graphical user interfaces (see Chapter 7)
Installing New Modules
In addition to the standard library modules, thousands of modules have been contributed by the Python
community. One very popular module is pygame, which we will use in Chapter 8. It’s often available
as a binary package, so you can install it by typing something like this:
For many modules, however, this is not the case, and you have to go through a bit more effort to
install them.
Any module good enough to use will be packaged in the standard Python way. This means that to
install it, you need to download a compressed file containing a directory for the module. Let’s use the
RPi.GPIO module we will use in Chapter 11 as an example. To install this module, you first go to the
module’s website, find the Downloads section, and then fetch the archive file. This is shown in Figure
5-1. The next step is to save the file to a convenient directory (for this example, use the Python
directory we created in Chapter 3).
Figure 5-1 Downloading the RPi.GPIO module
Once the file has been saved, open LXTerminal and use cd to get to the Python directory, like so:
Next, you need to extract the directory from the archive by entering the following command:
Now that you have a new folder for the module, you need to “cd” into it and then run the install