Technology Modules and Packages | Python Tutorial

Learn with diagrams, code, systems and practical examples.

</>
TutorialsPythonModules and Packages
Functions & Modules · Chapter 17

Modules and Packages

Split code into files and import reusable functionality.

Why this matters

As programs grow, modules help organize related behavior instead of keeping everything in one file.

Start with the idea

A Python file can act as a module. import lets one module use names from another. Packages organize related modules into a directory structure.

IdeaUnderstand the purpose
CodeRun the smallest example
PracticeChange it yourself
SubjectVision learning pattern: understand the idea before memorizing syntax.

Small working example

Run this example first. Do not change several things at once; confirm the basic behavior, then experiment.

Python
# math_helpers.py
def double(value):
    return value * 2

# app.py
from math_helpers import double
print(double(6))
Expected output
12
What to notice

The example is intentionally small. Focus on the chapter’s main idea before adding extra syntax or framework code.

Common beginner mistake

Mistake

Creating circular imports where modules depend on each other in a loop.

Better approach

Keep responsibilities clear and move shared contracts into a lower-level module when needed.

Quick recap

  • Modules are Python files.
  • Packages group modules.
  • Imports should reflect clean dependency direction.

Try it yourself

These are deliberately small. If you can complete them without copying the example, you are ready to continue.

  1. Move one function into a separate module.
  2. Import it from a small app file.