- Learning Objectives
- Prerequisites
- Introduction
- What is a Custom Module?
- Why Create Custom Modules?
- Creating Your First Module
- Importing Custom Modules
- Module Search Path
- Organizing Project Files
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Create your own Python modules.
- Import custom modules into other programs.
- Organize Python projects using modules.
- Understand how Python locates modules.
- Reuse code efficiently.
Before learning this chapter, you should know:
- Functions
- Classes
- Variables
- Introduction to Modules
As applications become larger,
keeping all the code inside one Python file makes the program difficult to read and maintain.
To solve this problem,
Python allows developers to divide applications into multiple reusable modules.
Each module is responsible for a specific task.
A Custom Module is a Python file (.py) created by the programmer to store reusable functions, classes, variables, and other program logic.
A custom module is a Python file created by the developer for code reuse.
Custom modules help developers:
- Reuse code.
- Improve readability.
- Reduce duplication.
- Organize projects.
- Simplify maintenance.
- Enable teamwork.
Example project structure:
Project
│
├── calculator.py
└── main.py
The reusable code is written inside
calculator.py
and imported into
main.py
# calculator.py
def add(a, b):
return a + b# main.py
import calculator
print(calculator.add(10, 20))30
Once a module is created,
it can be imported using
import module_nameExample
import calculatorPython searches for
calculator.py
and loads it into memory.
When Python imports a module,
it searches in the following locations:
- Current project directory.
- Directories listed in
PYTHONPATH. - Standard library directories.
- Installed third-party packages.
Example
StudentManagement
│
├── student.py
├── marks.py
├── attendance.py
└── main.py
Each file performs one specific responsibility.
This makes projects easier to understand and maintain.
Program Starts
↓
import calculator
↓
Python Searches calculator.py
↓
Module Loaded
↓
Module Executed
↓
Functions Available
↓
Program Continues
- Improves code reuse.
- Reduces duplication.
- Organizes projects.
- Simplifies debugging.
- Makes maintenance easier.
- Keep one responsibility per module.
- Use meaningful module names.
- Avoid unnecessary executable code.
- Group related functions together.
- Reuse existing modules whenever possible.
Creating very large modules.
Giving modules meaningless names.
Copying code instead of importing modules.
Saving modules outside the project search path.
A Python file created by the programmer to store reusable code.
To improve code organization, reusability, readability, and maintainability.
.py
import module_name- Current directory
- PYTHONPATH
- Standard library
- Installed packages
- Custom Module → Programmer-created Python file.
.pyextension.- Imported using
import. - Supports code reuse.
- Improves project organization.
In this chapter, you learned:
- Custom Modules
- Creating Modules
- Importing Modules
- Module Search Path
- Project Organization
- Advantages
- Best Practices
- Interview Questions
- Quick Revision
Create a custom module that contains a function to add two numbers.
def add(a, b):
return a + bimport calculator
result = calculator.add(10, 20)
print("Sum :", result)Sum : 30
The function add() is defined in calculator.py.
The main.py program imports the module and calls the function.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * bimport calculator
print(calculator.add(20, 10))
print(calculator.subtract(20, 10))
print(calculator.multiply(20, 10))30
10
200
A single module can contain multiple related functions.
company_name = "OpenAI"
location = "San Francisco"import company
print(company.company_name)
print(company.location)OpenAI
San Francisco
Modules can contain variables in addition to functions.
PI = 3.14159
GRAVITY = 9.8import constants
print(constants.PI)
print(constants.GRAVITY)3.14159
9.8
Modules are commonly used to store application-wide constants.
class Student:
def display(self):
print("Student Details")import student
obj = student.Student()
obj.display()Student Details
A module can contain classes and objects.
def add(a, b):
return a + bdef multiply(a, b):
return a * bimport addition
import multiplication
print(addition.add(5, 6))
print(multiplication.multiply(5, 6))11
30
A project can contain multiple modules,
each handling a specific responsibility.
def square(number):
return number * numberimport calculator
print(calculator.square(5))import calculator
print(calculator.square(10))25
100
The same module can be reused by multiple Python programs.
EmployeeManagement
│
├── employee.py
├── salary.py
├── department.py
└── main.py
Each module is responsible for one specific task.
This improves readability and maintenance.
import calculator
import constants
print(calculator.add(15, 25))
print(constants.PI)40
3.14159
A single Python program can import multiple custom modules.
class Employee:
def __init__(self, name):
self.name = name
def display(self):
print("Employee :", self.name)import employee
emp = employee.Employee("Rahul")
emp.display()Employee : Rahul
This example demonstrates a complete custom module containing a class that is imported and used by another program.
Program Starts
↓
Import Module
↓
Python Searches Module
↓
Module Loaded
↓
Functions / Variables / Classes Available
↓
Required Method Called
↓
Result Displayed
↓
Program Ends
Project
│
├── main.py
│
└── calculator.py
│
├── Variables
├── Functions
└── Classes
↓
Program Ends
- Promotes code reuse.
- Improves project organization.
- Reduces duplicate code.
- Simplifies debugging.
- Makes maintenance easier.
- Keep one responsibility per module.
- Use meaningful module names.
- Group related functions together.
- Avoid unnecessary executable statements in modules.
- Reuse modules instead of copying code.
Creating very large modules containing unrelated functionality.
Saving modules outside the project directory.
Using unclear or confusing module names.
Duplicating code instead of importing reusable modules.
A custom module is a Python file created by the programmer to store reusable code.
Yes.
A module can contain functions, classes, variables, and executable statements.
Yes.
Modules are designed for code reuse.
It improves readability, maintenance, and reusability.
Inside the project directory or another location included in Python's module search path.
Yes.
A Python program can import multiple built-in and user-defined modules.
- Code reuse
- Better organization
- Easier maintenance
- Reduced duplication
- Improved readability
- Create a calculator module.
- Create a module containing constants.
- Create a module containing a class.
- Import multiple custom modules into one program.
- Reuse the same module in multiple Python programs.
- Custom Module → Programmer-created
.pyfile. - Can contain functions, variables, classes, and executable statements.
- Imported using
import. - Supports code reuse.
- Improves project organization.
In this chapter, you learned:
- Creating Custom Modules
- Importing Custom Modules
- Modules with Functions
- Modules with Variables
- Modules with Classes
- Multiple Module Projects
- Module Reusability
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision