Modules in Python
A module is a Python file (.py) that contains code such as functions, variables, classes, or statements that can be reused in another Python program.
Modules help us organize code and reuse it instead of writing everything in one file.
1. Creating a Module
Suppose we create a file called calculator.py:
def add(a, b): return a + b def subtract(a, b): return a - b
Now calculator.py is a module.
2. Importing a Module
We can use the import keyword.
import calculator print(calculator.add(10, 5)) print(calculator.subtract(10, 5))
Output:
15 5
Structure
calculator.py ↓ import ↓ main.py
3. Using from
Instead of importing the complete module, we can import specific functions.
from calculator import add print(add(10, 5))
Output:
15
4. Import Multiple Functions
from calculator import add, subtract print(add(10, 5)) print(subtract(10, 5))
Output:
15 5
5. Import Everything
You can import everything from a module using *.
from calculator import * print(add(10, 5)) print(subtract(10, 5))
However, using * is generally not recommended in larger programs because it can make it unclear where names came from.
6. Module Alias
We can give a module a shorter name using as.
import calculator as calc print(calc.add(10, 20))
Output:
30
7. Built-in Modules
Python comes with many modules that are already available.
Some common modules:
| Module | Purpose |
|---|---|
math | Mathematical operations |
random | Random values |
datetime | Date and time |
os | Operating system operations |
sys | Python system information |
json | JSON data |
time | Time-related operations |
statistics | Statistical calculations |
8. math Module
The math module provides mathematical functions.
import math print(math.sqrt(25)) print(math.pi)
Output:
5.0 3.141592653589793
Other examples:
math.ceil(4.2) math.floor(4.8) math.pow(2, 3)
9. random Module
The random module generates random values.
import random number = random.randint(1, 10) print(number)
Example output:
7
The result can be different each time.
10. datetime Module
Used to work with dates and times.
import datetime today = datetime.datetime.now() print(today)
Example output:
2026-09-26 23:30:15.123456
11. os Module
The os module allows Python to interact with the operating system.
import os print(os.getcwd())
This displays the current working directory.
You can also check whether a file exists:
import os if os.path.exists("data.txt"): print("File exists")
12. sys Module
The sys module provides information and functions related to Python itself.
import sys print(sys.version)
This displays the Python version.
13. Module Variables
A module can also contain variables.
student.py
name = "Ram" age = 20
main.py
import student print(student.name) print(student.age)
Output:
Ram 20
14. Module Classes
A module can contain classes too.
student.py
class Student: def __init__(self, name): self.name = name def show(self): print(self.name)
main.py
from student import Student student = Student("Ram") student.show()
Output:
Ram
15. __name__
Every Python module has a special variable called __name__.
When a Python file is run directly:
print(__name__)
Output:
__main__
When the file is imported into another file, its __name__ is normally the module's name.
16. if __name__ == "__main__"
This is commonly used to make sure some code runs only when the file is executed directly.
calculator.py
def add(a, b): return a + b if __name__ == "__main__": print(add(10, 20))
If you run:
python calculator.py
Output:
30
But if another file imports calculator, the code inside the if block does not run automatically.
17. Standard Library Modules
Python includes a large standard library.
For example:
import math import random import datetime import os import json import statistics
These modules can be used without installing separate packages.
18. External Modules
Some modules/packages are not included with Python and need to be installed.
For example, requests can be installed using:
pip install requests
Then:
import requests
External packages are commonly used for web requests, data science, AI, development, and many other tasks.
19. Module vs Package
Module
Usually a single Python file:
calculator.py
Package
A collection of related Python modules organized in a directory.
Example:
myproject/ │ ├── main.py │ └── tools/ ├── calculator.py ├── converter.py └── validator.py
Here:
calculator.py → module converter.py → module validator.py → module tools/ → package
20. Why Use Modules?
Modules help to:
- Reuse code
- Organize large programs
- Avoid repetition
- Make code easier to maintain
- Separate different parts of a project
- Use existing Python functionality
21. Complete Example
calculator.py
def add(a, b): return a + b def multiply(a, b): return a * b
main.py
import calculator x = calculator.add(10, 20) y = calculator.multiply(5, 4) print("Addition:", x) print("Multiplication:", y)
Output:
Addition: 30 Multiplication: 20
Module Import Methods
| Method | Example |
|---|---|
| Import module | import math |
| Import specific item | from math import sqrt |
| Import multiple items | from math import sqrt, pi |
| Module alias | import math as m |
| Specific item alias | from math import sqrt as s |
| Import everything | from math import * |
Simple Structure
Python Modules │ ├── Built-in / Standard Library │ ├── math │ ├── random │ ├── datetime │ ├── os │ └── sys │ ├── User-Defined Modules │ └── calculator.py │ └── External Modules └── Installed using pip
In simple words: A module is a reusable Python file that helps you organize and reuse code.