- Learning Objectives
- Prerequisites
- Introduction
- What is a Package?
- Why are Packages Required?
- Package Structure
- Creating a Package
- init.py File
- Importing Packages
- Sub-Packages
- Package vs Module
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand packages in Python.
- Differentiate modules and packages.
- Create your own packages.
- Understand the purpose of the
__init__.pyfile. - Import modules from packages.
- Organize large Python projects using packages.
- Work with sub-packages.
Before learning this chapter, you should know:
- Modules
- Custom Modules
- Import Statements
- Aliasing
As applications become larger,
creating hundreds of modules in a single folder becomes difficult to manage.
Python solves this problem using Packages.
Packages allow developers to organize related modules inside folders, making projects easier to understand, maintain, and reuse.
A Package is a directory (folder) that contains one or more Python modules and is used to organize related code.
Traditionally, packages include an __init__.py file to indicate that the directory is a package.
A package is a folder that contains related Python modules.
Without packages,
large projects become difficult to organize.
Example:
Project
calculator.py
employee.py
student.py
salary.py
database.py
payment.py
inventory.py
products.py
orders.py
reports.py
notifications.py
Finding a specific module becomes difficult.
Using packages,
the same project becomes
Project
├── employee
│ ├── employee.py
│ ├── salary.py
│ └── attendance.py
│
├── sales
│ ├── orders.py
│ ├── products.py
│ └── reports.py
│
└── database
└── connection.py
This structure is easier to understand and maintain.
- Organize large projects.
- Reduce module name conflicts.
- Improve readability.
- Improve maintainability.
- Encourage modular programming.
- Support code reuse.
Example
Project
│
├── calculator
│ ├── __init__.py
│ ├── addition.py
│ ├── subtraction.py
│ └── multiplication.py
│
└── main.py
Step 1
Create a folder
calculator
Step 2
Create
__init__.py
Step 3
Add modules
addition.py
subtraction.py
multiplication.py
The folder now behaves as a package.
__init__.py is a special Python file placed inside a package.
Traditionally, it marks the directory as a Python package.
It can also contain initialization code that runs when the package is imported.
__init__.pyis the initialization file of a package.
Suppose
calculator
│
├── __init__.py
└── addition.py
contains
def add(a, b):
return a + bImport statement
from calculator import addition
print(addition.add(10, 20))from calculator.addition import add
print(add(15, 25))A package can contain another package.
Example
Company
│
├── HR
│ ├── employee.py
│ └── payroll.py
│
└── Sales
├── products.py
└── orders.py
These are called sub-packages.
| Module | Package |
|---|---|
Single .py file |
Folder containing modules |
| Stores reusable code | Organizes multiple modules |
| Smaller unit | Larger organizational unit |
Example: math.py |
Example: calculator/ |
Program Starts
↓
Import Package
↓
Python Searches Package
↓
__init__.py Executed
↓
Required Module Loaded
↓
Functions Available
↓
Program Continues
- Better project organization.
- Supports modular programming.
- Easier maintenance.
- Reduces naming conflicts.
- Encourages code reuse.
- Group related modules into packages.
- Use meaningful package names.
- Keep package hierarchy simple.
- Avoid circular imports.
- Organize packages based on functionality.
Putting unrelated modules inside the same package.
Creating very deep package hierarchies.
Using confusing package names.
Creating duplicate modules in multiple packages.
Project
├── calculator
│ ├── __init__.py
│ └── addition.py
└── main.py
def add(a, b):
return a + bfrom calculator import addition
print(addition.add(10, 20))30
from calculator.addition import add
print(add(50, 40))90
calculator
├── addition.py
├── subtraction.py
└── multiplication.py
from calculator import addition
from calculator import subtraction
print(addition.add(5, 5))
print(subtraction.subtract(10, 3))10
7
class Student:
def display(self):
print("Student Package")from school.student import Student
obj = Student()
obj.display()Student Package
Company
├── HR
│ └── employee.py
└── Sales
└── orders.py
from Company.HR import employeeimport calculator.addition
print(calculator.addition.add(7, 8))15
from calculator import addition
from calculator import multiplication
print(addition.add(2, 3))
print(multiplication.multiply(4, 5))5
20
import calculator.addition as add_module
print(add_module.add(100, 200))300
print("Calculator Package Loaded")import calculatorCalculator Package Loaded
Project
calculator
│
├── __init__.py
├── addition.py
├── subtraction.py
└── multiplication.py
main.py
from calculator.addition import add
print(add(25, 75))100
Program Starts
↓
Import Package
↓
Python Finds Package Folder
↓
Execute __init__.py
↓
Load Required Module
↓
Execute Function
↓
Display Output
↓
Program Ends
Project
│
├── Package
│ │
│ ├── __init__.py
│ ├── Module 1
│ ├── Module 2
│ └── Module 3
│
└── Main Program
A package is a directory containing related Python modules.
It initializes a package and traditionally identifies the directory as a Python package.
A module is a single .py file, whereas a package is a directory containing one or more modules.
Yes.
Such packages are called sub-packages.
Packages improve project organization, readability, maintainability, and code reuse.
Yes.
Packages contain modules, and modules can contain classes, functions, variables, and executable statements.
No.
It can contain initialization code, variables, or package-level imports if required.
- Create a calculator package.
- Create a student package.
- Create a package with multiple modules.
- Create a sub-package.
- Import modules using different techniques.
- Package → Folder containing modules.
- Module → Python file.
__init__.py→ Package initialization file.- Packages organize large projects.
- Packages can contain sub-packages.
- Import using package.module syntax.
In this chapter, we learned:
- Packages
- Package Structure
- Creating Packages
__init__.py- Importing Packages
- Sub-Packages
- Module vs Package
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision