Modules & Packages in Python
Learn how Python modules and packages help us organize and reuse code in different programs.
What is a Module?
A module is simply a Python file that contains Python code such as variables, functions and classes.
A module usually has the .py extension.
Instead of writing the same code again and again, we can place reusable code inside a module and use it in another Python program.
Example 📩🌍
Suppose we have a training institute application. We may keep student-related functions in one file, fee-related functions in another file and utility functions in another file.
student.py
Student-related functions and classes.
fees.py
Fee-related functions and calculations.
utility.py
Common reusable utility functions.
What is a Package?
A package is a collection of related Python modules organized inside a directory.
Packages help us organize larger applications into meaningful sections.
Module and Package Diagram
The following diagram shows the basic relationship between modules and packages.
Python Application
Large project containing reusable code
Package
Collection of related Python modules.
Module
A Python file containing reusable code.
Reusable Code
Functions, classes, variables and other Python code can be reused.
Creating a Simple Module
First, create a file named:
calculator.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
Here, calculator.py is a module
containing two reusable functions.
Using a Module
Now create another file named:
main.py
import calculator
print(calculator.add(10, 20))
print(calculator.subtract(20, 5))
Output:
30
15
The calculator module is imported into
main.py and its functions are reused.
Why Do We Use Modules?
- Reuse existing code.
- Avoid writing duplicate code.
- Keep programs organized.
- Make large programs easier to manage.
- Separate related functionality.
Basic Package Structure
A simple package structure can look like this:
myproject/
|
|-- main.py
|
|-- utilities/
| |
| |-- __init__.py
| |
| |-- calculator.py
| |
| |-- message.py
Here:
-
myprojectis the project folder. -
utilitiesis the package. -
calculator.pyandmessage.pyare modules. -
__init__.pyis traditionally used to mark and initialize the package.
Module vs Package
| Module | Package |
|---|---|
| Usually a single Python file. | Usually a directory containing related modules. |
Has a .py file extension.
|
Organizes multiple modules. |
| Contains functions, classes and variables. | Groups related modules together. |
Types of Python Modules
User-Defined Modules
Modules created by the developer,
such as calculator.py.
Built-in Modules
Modules provided by Python, such as
math and random.
Third-Party Packages
Packages installed using tools such as
pip.
Importing a Module
Python provides the import statement
to use a module.
import math
print(math.sqrt(25))
Output:
5.0
We will learn different import styles in detail in the next topic: Import Statement.
Benefits of Packages
- Organize large projects.
- Group related modules.
- Improve code maintainability.
- Make reusable components easier to manage.
- Help structure applications into logical sections.
Learning Flow
Step 1: Understand Modules
↓
Step 2: Learn Import Statement
↓
Step 3: Learn Built-in Modules
↓
Step 4: Learn pip & Packages
↓
Step 5: Create Your Own Packages
↓
Step 6: Learn Virtual Environments
Practice Programs
-
Create a
calculator.pymodule containing addition and subtraction functions. - Import the module into another Python file.
- Create a small package containing two modules.
-
Use a built-in Python module such as
math. - Explore the structure of a Python package.
Summary :
A module is a Python file containing reusable code. A package is a collection of related modules organized inside a directory. Modules and packages help developers reuse code, organize projects and manage large Python applications more effectively.