Python Modules
Table of Contents
In the last lesson, we learned about Python Magic Methods. Now let us learn how modules organize reusable Python code in separate files.
What Is a Module in Python?
A module is a file with a .py extension that contains Python code. It can contain functions, classes, variables, and even runnable code.
Think of a module like a toolbox. We put tools (functions, classes) inside. When we need a tool, we open the toolbox and take it out. Instead of writing the same code again and again, we put it in a module and reuse it.
Why Do We Need Modules in Python?
As our programs grow, they become large and messy. Putting everything in one file makes it hard to read and maintain.
Modules help us split our code into smaller, manageable files. Each file does one job. And we can reuse those files in other programs.
Python also comes with many built-in modules. We can use them without writing any code ourselves. For example, the math module has mathematical functions. The random module helps us generate random numbers.
How to Use a Built-in Module in Python
Let us start with a simple example. We will use the math module to calculate the square root of a number.
import math
result = math.sqrt(25)print(result)Output
5.0We imported the math module using import math. Then we used math.sqrt() to calculate the square root. The math module is built into Python, so we do not need to install anything.
How to Import a Module in Python
We use the import statement to bring a module into our program. The syntax is:
import module_nameThen we can use anything inside that module by writing module_name.something.
For example:
import random
number = random.randint(1, 10)print(number)Output (may vary):
7The random module has a function randint that gives a random integer between two numbers.
How to Create Our Own Module in Python
We can create our own module by writing a Python file. Let us create a file called mymodule.py with some functions.
def greet(name): return f"Hello, {name}!"
def add(a, b): return a + b
pi = 3.14159Now we can use this module in another file.
import mymodule
print(mymodule.greet("Alex"))print(mymodule.add(5, 3))print(mymodule.pi)Output
Hello, Alex!83.14159Both files must be in the same folder. When we run main.py, Python finds mymodule.py and imports it.
A Real-Life Example – A Utility Module
Let us create a utility module with common helper functions.
def is_even(number): return number % 2 == 0
def is_positive(number): return number > 0
def format_name(first, last): return f"{last}, {first}"Now we can use these functions in our main program.
import utils
print(utils.is_even(4))print(utils.is_positive(-5))print(utils.format_name("Alex", "Smith"))Output
TrueFalseSmith, AlexThat is how modules help us organize our code.
Common Mistakes with Modules in Python
Mistake 1: Forgetting to import the module
result = math.sqrt(25)This gives an error. Because we did not import math.
Fix:
import mathresult = math.sqrt(25)Mistake 2: Naming a file the same as a built-in module
If we create a file called math.py, it will shadow the built-in math module. That can cause problems.
Fix: Avoid using built-in module names for our files.
Mistake 3: Using a module without the dot
import mathresult = sqrt(25)This gives an error. Because sqrt is inside math. We need to use math.sqrt.
Fix:
result = math.sqrt(25)Mistake 4: Putting the module in the wrong folder
If mymodule.py is not in the same folder as our main script, Python will not find it.
Fix: Keep them in the same folder, or adjust the path.
Next, we will learn about the Python Import Statement and the different ways to bring module code into a program.