Key Takeaways: Modules, Packages, and the Python Ecosystem

One-Sentence Summary

Modules and packages let you organize code across multiple files, reuse code without duplication, and tap into Python's enormous ecosystem of pre-built tools — transforming single-file scripts into maintainable, professional projects.

Three Import Styles

Style Syntax When to Use
Full import import math You use many items from the module; clarity matters
Selective import from math import sqrt, pi You use 1-3 items; no name conflicts
Aliased import import datetime as dt Module name is long or has a community convention
Avoid from math import * Pollutes namespace — almost never appropriate

Standard Library Greatest Hits

Module What It Does Example
datetime Dates, times, durations datetime.now(), timedelta(days=7)
random Random numbers and selections randint(1, 6), choice(items)
collections Specialized containers Counter(data), defaultdict(list)
pathlib Modern file path handling Path("data") / "file.txt"
math Mathematical functions sqrt(), ceil(), pi
os / sys System interaction os.getcwd(), sys.path

Module vs. Package

Concept What It Is On Disk
Module A single .py file grading.py
Package A directory of related modules grading/ with __init__.py

The __name__ Guard

if __name__ == "__main__":
    # Only runs when this file is executed directly
    # Skipped when this file is imported by another file
  • Run directly: __name__ is "__main__"
  • Imported: __name__ is the module's name (e.g., "grading")

Key Rules

  1. Never name your files after standard library modulesrandom.py, math.py, os.py, etc. will shadow the real modules.
  2. Avoid circular imports — if A imports B and B imports A, restructure.
  3. Keep side effects out of module-level code — put initialization inside functions or __name__ guards.
  4. One responsibility per module — if you describe it with "and," split it.
  5. Check the standard library first — before writing a utility function, someone probably already wrote it better.

TaskFlow v1.1 Structure

File Responsibility
models.py Task creation, completion, search
storage.py JSON load/save
display.py Formatting and output
cli.py Menu and user input
main.py Entry point — orchestrates everything

What's Next

Chapter 13: Testing and Debugging — write automated tests for your modules. The modular structure you built here makes testing dramatically easier.