Organizing Python Code Beyond a Single Notebookยถ

Modules and packages help a project grow without becoming a mazeยถ
Functions gave your code reusable building blocks. Modules and packages decide where those building blocks live, how files talk to each other, and how a project stays understandable as more analysis, business rules, and models are added.
Why this mattersยถ
A project may begin as one notebook or one script, but it rarely stays that way. As soon as different parts of the work need their own space, structure becomes a quality issue, not just a style preference.
analysts may keep calculations in one file and reporting helpers in another
ML teams may separate data loading, feature logic, and model code
business applications often need configuration, APIs, and pricing rules in different places
teammates need clear file boundaries so they can change one area without breaking everything else
Continuity from the functions notebook
Functions answer: how do I reuse a block of logic? Modules and packages answer: where should that reusable logic live so the whole project stays organized?
Core Explanationยถ
A module is a single Python file that contains reusable code. A package is a folder that groups related modules so a project can be organized by responsibility instead of becoming one giant script.
Imports connect the piecesยถ
Imports are the links that turn many files into one program. They tell Python where to find the function, class, or module you want to reuse.
Dependency management in simple terms
Internal imports connect your own code. Dependencies are external packages, such as pandas or requests, that must also be installed so the project runs the same way for everyone on the team.
Visual intuition: growing from one file to a projectยถ
import statistics
values = [12, 18, 15, 21]
print(statistics.mean(values))16.5
Worked Example: from one script to a small business appยถ
A simple script may start in one file, but larger projects benefit from separating reusable logic into modules with clear responsibilities.
Example project layout
sales_forecasting_app/
โโโ app.py
โโโ data_layer/
โ โโโ __init__.py
โ โโโ loader.py
โโโ business_logic/
โ โโโ __init__.py
โ โโโ pricing.py
โโโ ml_models/
โ โโโ __init__.py
โ โโโ forecaster.py
โโโ config/
โโโ settings.pyIn that structure:
each module has one clear job
packages group related modules
imports define how parts of the system collaborate
configuration and external dependencies stay separate from business logic
A simple application entry point might look like this:
from data_layer.loader import get_data
from ml_models.forecaster import Forecaster
from business_logic.pricing import adjust_prices
def main():
data = get_data("sales_2024.csv")
model = Forecaster(model_type="baseline")
predictions = model.predict(data)
decisions = adjust_prices(predictions)
return decisionsGuided Checkยถ
What is a module in Python?ยถ
From Functions to Filesยถ
Once logic becomes reusable, the next step is to move it out of one notebook or script and into well-named modules and packages.
A clean project layout keeps responsibilities separate:
sales_forecasting_app/
โโโ app.py
โโโ data_layer/
โ โโโ __init__.py
โ โโโ loader.py
โโโ business_logic/
โ โโโ __init__.py
โ โโโ pricing.py
โโโ ml_models/
โ โโโ __init__.py
โ โโโ forecaster.py
โโโ config/
โโโ settings.pyIn that structure:
each module has one clear job
packages group related modules
imports define how parts of the system collaborate
configuration and external dependencies stay separate from business logic
A simple application entry point might look like this:
from data_layer.loader import get_data
from ml_models.forecaster import Forecaster
from business_logic.pricing import adjust_prices
def main():
data = get_data("sales_2024.csv")
model = Forecaster(model_type="baseline")
predictions = model.predict(data)
decisions = adjust_prices(predictions)
return decisionsThis is the practical reason imports matter: they let you compose one program from focused, reusable files instead of one growing script.
Exercisesยถ
Exercise 1: Identify the structureยถ
In your own words, explain the difference between:
a function
a module
a package
Hint
A function lives inside a file. A module is usually one file. A package is a folder that groups related modules.
Exercise 2: Read an importยถ
Look at this line and explain what each part means:
from analytics.cleaning import standardize_namesHint
analytics.cleaning is where Python looks. standardize_names is the reusable function being imported.
Exercise 3: Sketch a small projectยถ
Sketch a tiny business app structure with at least two packages and one import between them. You can do this in plain text.
Hint
Think about one package for data access and another for business rules or reporting.
Quick Summary
modules organize reusable code in files
packages group related modules together
imports connect pieces of code across files
dependency management helps projects stay reproducible
project structure matters more as applications grow
The next notebook focuses on making code more reliable through testing and debugging.
import math
print(math.sqrt(81))9.0
Practice Labยถ
Expected output
9.0Expected output
13Why these examples matter
These are standard-library imports. They show the everyday pattern of bringing reusable functionality into your code instead of rewriting it yourself.
Guided Practiceยถ
Why do programmers import modules?ยถ
Which module is used in the example to compute an average?ยถ
Key Takeawayยถ
As projects grow, structure stops being optional. Modules, packages, and dependencies are what let reusable functions become maintainable systems.