Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.py

In 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 decisions

Guided Checkยถ

What is a module in Python?ยถ

A Python file containing reusable codeCorrect. A module is typically one file with related logic.
A type of loopLoops repeat work; modules organize code.
A built-in chart typeModules are about program structure, not chart types.

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.py

In 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 decisions

This 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:

  1. a function

  2. a module

  3. 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_names
Hint

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.0
Expected output
13
Why 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?ยถ

To avoid using Python syntaxImporting modules does not remove Python syntax.
To access reusable functionalityCorrect. Modules provide organized, reusable code.
To rename variables automaticallyThat is not the main role of modules.
To turn lists into dictionariesImporting does not do that by itself.

Which module is used in the example to compute an average?ยถ

math`math` was used in the first example.
statisticsCorrect. `statistics.mean()` computes the average.
random`random` is for randomness, not averaging.
sys`sys` gives access to interpreter-related features.

Key Takeawayยถ

As projects grow, structure stops being optional. Modules, packages, and dependencies are what let reusable functions become maintainable systems.