Chapter 11. Modules, Packages, and Goodies
During your bottom-up climb, you’ve progressed from built-in data types to constructing ever-larger data and code structures. In this chapter, you finally learn how to write realistic whole programs in Python. You’ll write your own modules and learn how to use others from Python’s standard library and other sources.
The text of this book is organized in a hierarchy: words, sentences, paragraphs, and chapters. Otherwise, it would be unreadable pretty quickly.1 Code has a roughly similar bottom-up organization: data types are like words; expressions and statements are like sentences; functions are like paragraphs; and modules are like chapters. To continue the analogy, in this book, when I say that something will be explained in Chapter 8, in programming that’s like referring to code in another module.
Modules and the import Statement
We’ll create and use Python code in more than one file. A module is just a file of any Python code. You don’t need to do anything special—any Python code can be used as a module by others.
We refer to code of other modules by using the
This makes the code and variables in the
imported module available to your program.
Import a Module
The simplest use of the
import statement is
module is the name of another Python file,
without the .py extension.
Let’s say you and a few others want something fast for lunch, but don’t want a long discussion, and you always end ...