Copy as MarkdownCtrl+⇧+C
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.
| # | Lesson | Focus | SmartNotes Deliverable |
|---|---|---|---|
| 1 | Why Modules? | Single-file pain, import basics, creating modules | Split Note class into models.py, import it |
| 2 | Packages and __init__.py | Directory packages, __init__.py, public API | Create smartnotes/ package with submodules |
| 3 | Import Mechanics | Relative vs absolute imports, __name__ == "__main__" | Add smartnotes/__main__.py entry point |
| 4 | Avoiding Import Traps | Circular imports, star imports, namespace collisions | Diagnose and fix a circular import |
| 5 | SmartNotes Package Capstone | Full TDG cycle: package architecture | Refactored SmartNotes as a proper package |
Prerequisites: Chapters 42-62. You should be comfortable with classes, file I/O, and the TDG cycle.