Modular programming — modules and import
When a program gets bigger, it is not practical to write all the code in one file. That is why we use modular programming.
Modular programming means dividing a program into smaller parts — separate Python files called modules. Each module contains functions and variables that deal with one specific thing.
For example:
- one module can do maths calculations
- another module can work with text
- the main program uses those modules
This way the program becomes more readable, easier to understand and easier to extend.
Why use modules?
- Better organisation — the code is divided into logical parts
- Reuse — you can use the same module in several projects
- Easier testing — you check one part at a time
- Fewer mistakes — you do not repeat the same code several times
How to make and use your own module
A module is an ordinary Python file (.py).
We write functions in it and then use it in another file.
# file: helper_functions.py
def greet():
print("Hello from the module!")
In the main program we bring in that module with import.
# file: main.py
import helper_functions
helper_functions.greet()
In Thonny it is important that both files are in the same folder.
You run the file main.py, and the function runs from the module.
Different ways of importing a module
There are several ways to import a module, depending on what you need.
1. A plain import
import math
print(math.sqrt(16))
You call functions by writing module_name.function_name.
2. Import with an alias (a shorter name)
import math as m
print(m.sqrt(25))
An alias is often used to make the code shorter to write.
3. Importing a specific function from a module
from math import sqrt
print(sqrt(36))
Here you do not need to write the module name in front of the function.
Example: several functions in one module
# file: maths.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
# file: program.py
import maths
print(maths.add(3, 4))
print(maths.multiply(5, 6))
This way a module becomes a small "library" of functions.
Packages (a short introduction)
When a project gets bigger, we can group modules into packages (folders).
project/
│
├── main.py
└── tools/
├── __init__.py
└── maths.py
Then we import the module like this:
from tools.maths import add
print(add(10, 5))
For now it is enough that you understand the idea — packages are used for even better organisation of larger projects.
Good practices and common mistakes when working with modules
When you use modules, it is important to know not only how to import them, but also how to use them correctly. These are the most important tips for beginners.
Good practices
- One module — one purpose
A module should do one main thing. For example: one module for maths, another for working with text. - Clear and meaningful file names
The name of a module should describe what is in it (maths.py,text_tools.py). - Using import without the asterisk
It is better to write:import maths
than:from maths import * - Functions with a clear role
Each function should do one thing and have an understandable name. - Comments and short descriptions
A short comment at the start of a module helps others (and you later) understand what the module is for.
Common beginner mistakes
- The files are not in the same folder
If Python cannot find a module, check whether the files are in the same folder. - A wrong module name
The file name must match the name in theimportstatement exactly. - Forgetting the module name when calling a function
If you use a plainimport, you must call the function like this:module_name.function_name() - Copying code instead of using a module
If you copy the same code several times — that is a sign you should make a module.
Important note
If you see the message ModuleNotFoundError,
it means that Python cannot find the module.
The most common cause is the wrong file location or a wrong name.
Modular programming is learnt step by step — the more of your own modules you write, the more natural this way of working will feel.
Hidden mini quiz — Python modules and import
Click to check whether you understand how
modules, the different forms of the import statement
and organising code in several files work.
Open the quiz
Mini quiz: Modules and import
1. What is a Python module?
2. How do you correctly call the function greet from the module helpers?
import helpers
3. What does the following statement do?
from math import sqrt
4. What is a good reason to use modules?
5. Where is the file you import as a module usually located (in Thonny)?
Tasks for independent work — Modules and import (7th and 8th grade)
The tasks go from easy to harder. The goal is to practise on your own making modules, importing functions and organising code.
- Easy task — a simple module
Make a module
greetings.pywith a functiongreet()that prints your name and a greeting. In the main filemain.pyimport the module and call the function. - A module with several functions
Make a module
maths.pywith the functionsadd(a, b)andsubtract(a, b). In the main program call both functions with a few examples and print the results. - Import with an alias
Make a module
text_tools.pywith a functionreverse_string(s). In the main program import the module using the aliasimport text_tools as ttand call the function through the alias. - Several files and functions
Make two files: a module
numbers.pywith the functionslarger(a,b)andsmaller(a,b), andprogram.py, which uses both functions and prints the results.Hint: Make sure the files are in the same folder and that you use
import. - Competition level — a mini library of functions
Make a module
tools.pythat contains at least 3 different functions of your choice (e.g. calculating a square, adding, processing text). Make a main program that imports the module and uses all the functions.Hint: Make sure the function names are clear and logical. Think about how you would organise the code so that you can add new functions later without problems.
Tip: If you are not sure how importing works, make small tests: call the functions and look at the results. Gradually add new functions and modules.
The next step — applying your knowledge in a project
In this lesson you learnt how to use functions and how to organise code with modules. The next logical step is to apply all of this in a real program.
In the next lesson we will do a mini project in which we rework the well-known game "Guess the number" so that:
- the code is divided into several functions
- the program uses several files (modules)
- the game logic is clearer and easier to extend
This project is meant for students who want to go a step further and work like real programmers.
