🔷 Introduction

As your Python projects grow, writing everything in a single file becomes messy and hard to maintain. Professional developers organize code into modules and packages to build scalable and maintainable applications.

In this lesson, you will learn:

✔ What modules and packages are
✔ How to import and organize code
✔ How to structure real-world Python projects
✔ Absolute vs relative imports
✔ Best practices for clean architecture

🟩 1. What is a Module?

A module is simply a Python file (.py) containing functions, classes, or variables.

🔹 Example

File: math_utils.py

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

def multiply(a, b):
    return a * b

Using the module:

import math_utils

print(math_utils.add(2, 3))

🔹 Import specific functions

from math_utils import add

print(add(5, 3))

🔹 Rename module (alias)

import math_utils as mu

print(mu.multiply(2, 4))

🟩 2. What is a Package?

A package is a folder that contains multiple modules.

my_project/
│
├── main.py
├── utils/
│   ├── __init__.py
│   ├── math_utils.py
│   └── file_utils.py

👉 __init__.py tells Python this folder is a package.

🟩 3. Absolute vs Relative Imports

🔹 Absolute Import (recommended)

from utils.math_utils import add

✔ Clear and readable
✔ Preferred in large projects

🔹 Relative Import

from .math_utils import add

✔ Used inside packages
✔ Less common for beginners

🟩 4. Organizing a Real Project

🔹 Example Structure

project/
│
├── app/
│   ├── __init__.py
│   ├── main.py
│   ├── services/
│   │   ├── __init__.py
│   │   └── user_service.py
│   ├── models/
│   │   └── user.py
│   └── utils/
│       └── helpers.py
│
├── tests/
├── requirements.txt
└── README.md

💡 Why structure matters

✔ Easier maintenance
✔ Better readability
✔ Scalable architecture
✔ Team collaboration

🟩 5. The __init__.py File

🔹 Purpose

🔹 Example

from .math_utils import add

🟩 6. The __name__ == "__main__" Pattern

🔹 Why it matters

Allows a file to be used as:

🔹 Example

def main():
    print("Running main")

if __name__ == "__main__":
    main()

🟩 7. Best Practices

✔ Use meaningful module names
✔ Avoid circular imports
✔ Keep files small and focused
✔ Group related logic
✔ Use virtual environments
✔ Add requirements.txt

🟧 8. Exercises (Hidden Solutions)

Exercise 1 — Create a module

Create a file with a function and import it.

Exercise 2 — Create a package

Exercise 3 — Absolute import

Exercise 4 — Use name

Exercise 5 — Organize a project

🟦 Conclusion

In this lesson, you learned how to:

✔ Structure Python code using modules and packages
✔ Use imports effectively
✔ Organize large projects
✔ Follow professional development practices

These concepts are critical for:

🚀 Mini Project — CLI Task Manager with Real Project Structure

Project idea

Build a simple command-line Task Manager that allows the user to:

This project is excellent for practicing:

📁 Project Structure

task_manager/
│
├── main.py
├── data/
│   └── tasks.txt
├── app/
│   ├── __init__.py
│   ├── task_service.py
│   ├── file_service.py
│   └── models.py
└── README.md

 

🧩 What each file does

✅ Full Solution

🔍 Why this project is useful

This mini project teaches you  how to organize a real Python app by separating:

That is exactly how larger applications are built.

🛠 Concepts practiced

With this project, you practice:

📌 Possible improvements

You can extend the project with:

  1. delete a task
  2. edit a task
  3. add due dates
  4. store tasks in JSON instead of TXT
  5. add task priority
  6. create a GUI version later