Skip to main content

Chapter 63 — Modules and Packages

SmartNotes has grown: a Note class, search functions, a FileManager, statistics, decorators, and tests. All crammed into a handful of files in one directory. This works for learning. It does not work for a real project.

This chapter teaches you to organize Python code into modules (individual .py files) and packages (directories with __init__.py). You will split SmartNotes into a proper package with separate modules for models, search, storage, and tests.

#LessonFocusSmartNotes Deliverable
1Why Modules?Single-file pain, import basics, creating modulesSplit Note class into models.py, import it
2Packages and __init__.pyDirectory packages, __init__.py, public APICreate smartnotes/ package with submodules
3Import MechanicsRelative vs absolute imports, __name__ == "__main__"Add smartnotes/__main__.py entry point
4Avoiding Import TrapsCircular imports, star imports, namespace collisionsDiagnose and fix a circular import
5SmartNotes Package CapstoneFull TDG cycle: package architectureRefactored SmartNotes as a proper package

Prerequisites: Chapters 42-62. You should be comfortable with classes, file I/O, and the TDG cycle.