Next / Previous / Contents / TCC Help System / NM Tech homepage

9. Using Python modules

Once you start building programs that are more than a few lines long, it's critical to apply this overarching principle to programming design:

Important

Divide and conquer.

In other words, rather than build your program as one large blob of Python statements, divide it into logical pieces, and divide the pieces into smaller pieces, until the pieces are each small enough to understand.

Python has many tools to help you divide and conquer. In Section 8, “def: Defining functions”, we learned how to package up a group of statements into a function, and how to call that function and retrieve the result.

Way back in Section 2.3, “More mathematical operations”, we got our first look at another important tool, Python's module system. Python does not have a built-in function to compute square roots, but there is a built-in module called math that includes a function sqrt() that computes square roots.

In general, a module is a package of functions and variables that you can import and use in your programs. Python comes with a large variety of modules, and you can also create your own. Let's look at Python's module system in detail.

9.1. Importing items from modules

Back in Section 2.2, “The assignment statement”, we learned that there is an area called the “global namespace,” where Python keeps the names and values of the variables you define.

The Python dir() function returns a list of all the names that are currently defined in the global namespace. Here is a conversational example; suppose you have just started up Python in conversational mode.

>>> dir()
['__builtins__', '__doc__', '__name__']
>>> frankness = 0.79
>>> dir()
['__builtins__', '__doc__', '__name__', 'frankness']
>>> def oi():
...     print "Oi!"
... 
>>> dir()
['__builtins__', '__doc__', '__name__', 'frankness', 'oi']
>>> type(frankness)
<type 'float'>
>>> type(oi)
<type 'function'>
>>> 

When Python starts up, three variables are always defined: __builtins__, __doc__, and __name__. These variables are for advanced work and needn't concern us now.

Note that when we define a variable (frankness), next time we call dir(), that name is in the resulting list. When we define a function (oi), its name is also added. Note also that you can use the type() function to find the type of any currently defined name: frankness has type float, and oi has type function.

Now let's see what happens when we import the contents of the math module into the global namespace:

>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frankness', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'oi', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>> sqrt(64)
8.0
>>> pi*10.0
31.415926535897931
>>> cos(0.0)
1.0
>>> 

As you can see, the names we have defined (oi and frankness) are still there, but all of the variables and functions from the math module are now in the namespace, and we can use its functions and variables like sqrt() and pi.

In general, an import statement of this form copies all the functions and variables from the module into the current namespace:

from someModule import *

However, you can also be selective about which items you want to import. Use a statement of this form:

from someModule import item1, item2, ...

where the keyword import is followed by a list of names, separated by commas.

Here's another example. Assume that you have just started a brand new Python session, and you want to import only the sqrt() function and the constant pi:

>>> dir()
['__builtins__', '__doc__', '__name__']
>>> from math import sqrt, pi
>>> dir()
['__builtins__', '__doc__', '__name__', 'pi', 'sqrt']
>>> sqrt(25.0)
5.0
>>> cos(0.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'cos' is not defined
>>> 

We didn't ask for the cos() function to be imported, so it is not part of the global namespace.