Creating a package
A package is a directory that contains module files plus one additional file. Each package must have an __init__.py
file. This file must be present and is often empty.
The poem, Zen of Python, by Tim Peters, offers the following advice:
Flat is better than nested.
The idea is to organize Python applications into a flat collection of modules to the greatest extent possible. A deeply-nested, complex hierarchy of packages isn't considered helpful.
We can use a package in two ways. We can import a module that's part of a package. The standard library, for example, has an XML package with several XML parser modules. We can use import xml.etree
to import the etree
module from the XML package. In this case, the __init__.py
file has a comment and a list of sub-packages.
In other cases, we can import the package, as a whole, as if the package were a module. When we write import collections, for example, we're really importing the module collections/__init__.py
.
The __init__...