liveBook · Manning

Chapter 18: Packages

What you will learn in chapter 18 of The Quick Python Book, Fourth Edition

This introduction explains why packages help organize related modules and distinguishes that meaning of package from a distributable file.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why packages help organize related modules and distinguishes that meaning of package from a distributable file.

Key terms: module, package, distributable file

18.1 What is a package?

Defines packages by contrasting their directory-based structure and purpose with modules.

Key terms: module, package, main package directory, modules, packages, related modules

18.2 A first example: mathproj

The mathproj example shows how a large Python project can be divided into directories and files whose structure helps organize and name its code.

Key terms: package, mathproj, ui, comp, symbolic, numeric, constants.py, pi, PiClass

18.3 Implementing the mathproj package

The section introduces the mathproj example and explains how to prepare its files and a clean Python session for the chapter’s examples.

Key terms: file, .py, module, mathproj, Colaboratory, setup cell, current working directory, Python namespace, import statements, new session

18.3.1 __init__.py files in packages

This subsection explains how __init__.py files mark package directories and provide a place for initialization code.

Key terms: __init__.py, package, subpackage, package initialization

18.3.2 Basic use of the mathproj package

Shows what happens when mathproj is first loaded and how to access an attribute defined in the package.

Key terms: mathproj/__init__.py, package, first loaded, version, mathproj package namespace, attributes

18.3.3 Loading subpackages and submodules

This subsection shows that importing a package does not load all its submodules, and that importing a nested module loads its parent packages first.

Key terms: top-level module, submodules, mathproj.comp.numeric.n1, subpackage, __init__.py, mathproj.comp, mathproj.comp.numeric

18.3.4 import statements within packages

This subsection explains how modules inside a package import objects from other package files, including when relative imports work.

Key terms: package, module, import statements, version, h, relative import, package hierarchy, __name__, __main__

18.4 The __all__ attribute

This section explains how __all__ controls wildcard imports from a package, why explicit names matter, and why wildcard imports do not recurse into subpackages.

Key terms: __all__, __init__.py, from ... import *, case, subpackages, mathproj, comp, c1, recursive importing

18.5 Proper use of packages

This section recommends keeping package layouts shallow and avoiding inconsistent use of __all__ to hide names.

Key terms: packages, deeply nested directory structures, top-level directory, Flat is better than nested, __all__ attribute, from ... import*, underscore

18.6 Creating a package

This section asks the reader to turn an earlier text-processing module into a package with separate responsibilities and a main function that uses its modules.

Key terms: package, module, cleaning functions, processing functions, custom exceptions, main function

18.6.1 Solving the problem with AI-generated code

This subsection says the lab’s main challenge is dividing work among functions and allows more than one reasonable division.

Key terms: function, division

18.6.2 Solutions and discussion

18.7 Summary

Concepts explained in this chapter

← Chapter 17: Data types as objects Chapter 19: Using Python libraries →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.