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 ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.