Python Modules vs Packages

Understand the difference with real code examples and mental models

Why Modules and Packages Exist?

As Python projects grow, writing all code in a single file becomes difficult to manage. Python solves this using modules and packages to organize code logically, cleanly, and professionally.

1️⃣ Python Module

A module is a single Python file (.py) that contains variables, functions, or classes.

Example: Module File

# math_utils.py  (This is a MODULE)

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b
      

Using the Module

import math_utils

print(math_utils.add(5, 3))
print(math_utils.subtract(10, 4))
      

✔ One file
✔ Easy to reuse
✔ Best for small or focused functionality

2️⃣ Python Package

A package is a folder that contains multiple modules and an optional __init__.py file.

Package Folder Structure

student/                 ← PACKAGE
│
├── __init__.py
├── marks.py
├── attendance.py
      

marks.py

def total_marks():
    return 450
      

attendance.py

def percentage():
    return 92
      

__init__.py

from .marks import total_marks
from .attendance import percentage

__all__ = ["total_marks", "percentage"]
      

Using the Package

from student import total_marks, percentage

print(total_marks())
print(percentage())
      

✔ Multiple files
✔ Better structure
✔ Used in real-world projects

Module vs Package – Comparison Table

Feature Module Package
Definition Single Python file Folder of modules
Extension .py Directory
Contains Functions, variables, classes Modules & sub-packages
__init__.py Not required Used to initialize package
Project Size Small Medium to Large
Real Use Utility files Frameworks & applications

Mental Model to Remember

If your project feels “too big for one file”, you need a package.