A Python project – A modular project (code organisation)
In this project you will learn how to organise a Python application using modules and several files.
Instead of one big file, we will split the code into logical parts.
- smaller and clearer code
- easier maintenance
- reusing functions
Why is modular programming important?
Large programs become hard to follow if they are in one file.
That is why we use modules — smaller pieces of code that each solve one problem.
- easier debugging
- better organisation
- teamwork
Project structure
project/
│
├── main.py
├── math_utils.py
├── file_utils.py
1. A module for maths
# math_utils.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
2. A module for working with files
# file_utils.py
def save_result(result):
with open("result.txt", "w") as f:
f.write(str(result))
3. The main program
# main.py
from math_utils import add
from file_utils import save_result
result = add(10, 5)
print("Result:", result)
save_result(result)
How does this work?
- import connects the files
- each file has its own role
- the main program uses the modules
Common mistakes
- ❌ a wrong file name
- ❌ a forgotten import
- ❌ a clash of function names
- ❌ not all files are in the same folder
How can you extend the project?
- add more modules
- make folders (packages)
- add a configuration file
- make a CLI application
Mini challenges
- Add a function for multiplication
- Add reading from a file
- Organise the code into a folder (package)
Where is the modular approach used?
- web applications
- backend systems
- games
- data science projects
Modularity makes it possible to develop larger and more complex systems.
Related lessons
Frequently asked questions
What is a module in Python?
A file that contains Python code and functions we can reuse.
Why use several files?
Because the code is clearer and easier to maintain.
When should I start with modular code?
As soon as the project grows beyond one file.
1) first run the given code without changes, 2) change one thing and predict what will happen, 3) add at least one feature of your own, 4) write down in a notebook what you changed and why. A work log like that is the basis of a creative programming paper.
