Chapter 22. Modules: The Big Picture
This chapter begins our in-depth look at the Python module—the highest-level program organization unit, which packages program code and data for reuse, and provides self-contained namespaces that minimize variable name clashes across your programs. Modules were introduced in Chapter 3, and we’ve been using them more and more since Chapter 16, but this part of the book provides a focused, detailed look at this Python tool.
This first chapter in Part V reviews module basics, and offers a general look at the role of modules in the overall structure of programs. In the chapters that follow, we’ll dig into the coding details behind that theory. Along the way, we’ll also flesh out module fine points omitted so far—you’ll learn about reloads, the __name__ and __all__ attributes, package imports, relative import syntax, namespace packages, the __getattr__ hook, the __main__.py file, and so on. Because modules and classes are really just glorified namespaces, this part formalizes namespace concepts as well.
Module Essentials
In simple and concrete terms, modules typically correspond to Python source code files. Each file of code is a module automatically, and modules import other modules to use the names they define. Modules might also correspond to extensions coded in external languages such as C, Java, or C#, and even to entire directories in package imports, which extend the model for nested files. In all their forms, modules are processed with ...