Modules and Packages in Python
As Python programs become larger, it becomes important to organize your code properly. Modules and packages help you structure your code, reuse functions, and keep everything clean and manageable. This lesson explains how modules and packages work, how to create them, and how to use built-in modules in your programs.
What Is a Module?
A module is simply a Python file that contains code — functions, variables, or classes — that you can reuse in another program. Instead of rewriting code again and again, you import the module and directly use it.
Example: Python's math, random, datetime modules.
Why Do We Use Modules?
- To reuse code across multiple programs
- To keep code clean and organized
- To avoid rewriting the same functions again
- To use Python’s powerful built-in features
Importing a Module
You can import a module using the import keyword.
import math
print(math.sqrt(25))
print(math.pi)
Here the math module gives access to many mathematical functions. The copy button is placed with enough margin to avoid overlapping with code.
Importing Specific Functions
You can import only what you need from a module:
from math import sqrt, ceil
print(sqrt(49))
print(ceil(4.3))
This keeps your code shorter and cleaner.
Giving a Module a Nickname (Alias)
You can rename a module using as:
import math as m
print(m.pi)
print(m.factorial(5))
Aliases help make your code readable, especially when the module name is long.
Creating Your Own Module
You can create your own module simply by making a new Python file.
Step 1: Create a file named mypython.py
# mypython.py
def greet(name):
return f"Hello, {name}!"
pi_value = 3.14
Step 2: Import and use it in another program:
import mypython
print(mypython.greet("Dataplexa"))
print(mypython.pi_value)
What Is a Package?
A package is a collection of multiple modules stored inside a folder.
In Python, a folder becomes a package when it contains a special file: __init__.py
A package = folder + modules
Structure of a Package
myproject/
math_tools/
__init__.py
add.py
subtract.py
multiply.py
main.py
Using a Module from a Package
You can import modules from inside a package like this:
from math_tools.add import add_numbers
print(add_numbers(10, 20))
Common Built-in Python Modules
- math – mathematical functions
- random – random number generation
- datetime – date and time handling
- os – operating-system related operations
- sys – system-specific information
Real-World Example: Generating OTP
The random module can be used to generate a simple OTP.
import random
otp = random.randint(100000, 999999)
print("Your OTP is:", otp)
📝 Practice Exercises
Exercise 1
Create a module named utils.py with a function square(num).
Import it into another file and print the square of a number.
Exercise 2
Use the random module to pick a random item from a list.
Exercise 3
Import only the pi value from the math module and use it to compute the area of a circle.
Exercise 4
Create a package named calculator with modules for add, subtract, multiply.
Import and use them in a main program.
✅ Practice Answers
Answer 1
# utils.py
def square(num):
return num * num
# main.py
import utils
print(utils.square(6))
Answer 2
import random
items = ["apple", "banana", "grapes"]
print(random.choice(items))
Answer 3
from math import pi
radius = 5
area = pi * radius * radius
print(area)
Answer 4
# calculator/add.py
def add(a, b):
return a + b
# calculator/multiply.py
def multiply(a, b):
return a * b
# calculator/subtract.py
def subtract(a, b):
return a - b
# main.py
from calculator.add import add
from calculator.multiply import multiply
print(add(10, 20))
print(multiply(3, 4))