This article introduces the idea of importing Python modules that manage many common and/or special tasks that you would otherwise have to write programming code for. IDLE is featured in this article because of some of its handy features, but you can follow the examples in other ways as well.
The material in this article directly supplements the official Python Tutorial, and pretty much assumes you have at least skimmed over it or gotten the same information elsewhere. This article is intended to help you “get busy coding” right away and show you a few essential tricks.
WHAT A MODULE IS: |
---|
The short answer is that a module is a text file containing one or more Python statements and/or definitions (of functions and classes). Most modules also contain comments. A statement is pretty much one or more lines of Python code that give Python orders. (The classic example of a first statement for programming students to write is to tell Python to print “Hello world”, but statements can be quite a bit more sophisticated.) When you import a module, statements not included within the definitions of functions and classes are executed. These statements may even involve importing other modules in order to work. |
IMPORTING BASICS: |
Importing a Python module is usually more simple than you might think. For instance, if you want to use the module named “time” in your program, you merely enter the line:import time If you wanted to import everything the time module has to export, but not import the time module itself, you could simply type: from time import * If you only wanted to import isleap() from the calendar module, all you would need to do is to type:
See if you can follow what’s happening in the following code: >>> import time See if you can tell in what way that differs from the following: >>> from time import * One difference between the two approaches is that by importing * from the time module, you are importing each of the resources of the time module for individual use, so you don’t have to qualify them by tacking “time.” onto the front. Sometimes this is a good idea, but you want to make sure not to have two objects “by the same name in the same place”, in a manner of speaking. Spend some time experimenting with different modules, and see if you can find out for yourself what that means. |
SEVERAL WAYS TO LEARN YOUR WAY AROUND MODULES: |
Python Documentation is usually bundled with Python when it is installed on your computer. It may also be found online at http://python.org/doc/ for the current release of Python as well as archives going back several years. Here you can find the official Python Tutorial, Module Index, Library Reference, and more. You may also find handy utilities like pydoc (which may be labeled “Module Docs” or similar) either included in your distribution or available through other sources. Using such a utility, you can search for modules on your local machine just like you would use a search engine such as google.com on the web. Opening Modules in IDLE or your preferred text editor can expose many details not described elsewhere. Remember, modules are saved as text files, so you can read them without the use of special tools. Open them in the usual way with your favorite text editor/viewer, or select File>Open module from IDLE (the *Python Shell*). Just remember to open the files ending in extensions other than .pyc, because .pyc files are not plain text files, but files compiled for execution. And fiddling with example code is definitely one of the best ways to learn what you can do with specific modules and the tools they offer. You should do as much code tinkering as you can in order to keep learning. This fiddling is the heart and soul of Useless Python! Once you have imported a module into your program (including an interactive interpreter session, such as IDLE), you can perform dir() on it to list the names of the module’s contents. Just remember this will not work until after you have imported the module. Many modules contain enough items that dir() will unattractively dump a listing that will be very little fun to read. In the following example, also note how dir() is correctly used to display the contents of the calendar module at the IDLE prompt:
Not too pretty, eh? Even worse, it really isn’t easy to read and comprehend in this form. Fortunately, it is extremely easy to print this information out in a format that is both appealing and easy to follow with a simple “for” loop:
Among its many uses, dir() can be a great quick-look-up tool to refresh your memory when you find yourself on the hunt for just the right module or function. But if you think that’s impressive, help() should really blow you away! You don’t have to use any fancy “for” loops with help(), because help() provides a wealth of descriptive information about the module and its contents. Try help() for plenty of information in a flash.
This is an example of help() usage, but it provides so much information that I encourage you to experiment with it in your own favorite setting to see the output. |