Chapter 18. Packages
This chapter covers
- Defining a package
- Creating a simple package
- Exploring a concrete example
- Using the __all__ attribute
- Using packages properly
Modules make reusing small chunks of code easy. The problem comes when the project grows and the code you want to reload outgrows, either physically or logically, what would fit into a single file. If having one giant module file is an unsatisfactory solution, having a host of little unconnected modules isn’t much better. The answer to this problem is to combine related modules into a package.
A module is a file containing code. A module defines a group of usually related Python functions or other objects. The name of the module is derived from the name of the file.
When you understand modules, packages are easy, because a package is a directory containing code and possibly further subdirectories. A package contains a group of usually related code files (modules). The name of the package is derived from the name of the main package directory.
Packages are a natural extension of the module concept and are designed to handle very large projects. Just as modules group related functions, classes, and variables, packages group related modules.