In this chapter, we looked at the details of how Python modules and packages work. We saw that modules are simply Python source files that get imported using an
import statement, and that packages are directories of Python source files identified by a package initialization file named
__init__.py. We learned that packages can be defined inside other packages to form a tree-like structure of nested packages. We looked at how modules and packages can be initialized, and how the
import statement can be used in various ways to import modules and packages, and their contents, into your programs.
We then saw how relative imports can be used to import modules relative to your current position in the package hierarchy and how the
__all__ variable can be used to control what gets included in an import.
We then learned about circular dependencies and how to avoid them, and we finished by learning about chameleon modules, which can act as both importable modules and as standalone programs that...