🔷 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 * bUsing 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
- Marks a directory as a package
- Can initialize package-level code
🔹 Example
from .math_utils import add🟩 6. The __name__ == "__main__" Pattern
🔹 Why it matters
Allows a file to be used as:
- a script
- a module
🔹 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:
- Django projects
- APIs
- Large-scale applications
- Team development
🚀 Mini Project — CLI Task Manager with Real Project Structure
Project idea
Build a simple command-line Task Manager that allows the user to:
- add a task
- view all tasks
- mark a task as completed
- save tasks to a file
This project is excellent for practicing:
- modules
- packages
- imports
- project organization
- separation of concerns
📁 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
- main.py → entry point of the program
- app/models.py → defines the task structure
- app/task_service.py → handles task operations
- app/file_service.py → handles file reading/writing
- data/tasks.txt → stores tasks
- app/init.py → marks
appas a package
✅ Full Solution
🔍 Why this project is useful
This mini project teaches you how to organize a real Python app by separating:
- data model
- business logic
- file handling
- main program flow
That is exactly how larger applications are built.
🛠 Concepts practiced
With this project, you practice:
- creating modules
- creating packages
- importing functions from different files
- using
__init__.py - organizing a real project structure
- using
if __name__ == "__main__" - splitting responsibilities across files
📌 Possible improvements
You can extend the project with:
- delete a task
- edit a task
- add due dates
- store tasks in JSON instead of TXT
- add task priority
- create a GUI version later
💬 Comments
No comments yet. Be the first to comment!
Login to comment.