4.3 MODULE, PACKAGE AND
LIBRARY IN PYTHON
Introduction
Python provides reusable code using
modules, packages, and libraries. These help programmers write organized, reusable,
and efficient programs.
Easy Memory Trick
|
Term |
Meaning |
Example |
|
Module |
Single
Python file |
math.py |
|
Package |
Folder
containing modules |
numpy.random |
|
Library |
Collection
of packages and modules |
NumPy,
Pandas |
Easy Analogy
Library = House
Package = Room
Module = Book
A house contains rooms, and rooms
contain books.
Similarly:
Library → Packages → Modules
MODULE
Definition
A module is a single Python file (.py) containing reusable code such as
functions, classes, variables, and statements.
Modules help reduce repetition and
make programs organized.
Types of Modules
1. Built-in Module
A built-in module is a module
already provided by Python that contains predefined functions, classes, and
variables to perform specific tasks.
These modules are available
automatically after Python installation.
Examples
·
math
·
random
·
os
·
sys
·
datetime
Example Program
import math print(math.sqrt(25))
Output
5.0
2. User-defined Module
A user-defined module is a module
created by the programmer to store reusable code such as functions, classes,
and variables in a separate Python file.
Example
# calculator.py def add(a, b): return a + b
Using the Module
import calculator print(calculator.add(5, 3))
Output
8
Advantages of Modules
1. Reuse
code multiple times
2. Reduce
code duplication
3. Make
programs organized
4. Simplify
testing and debugging
5. Improve
readability
Methods of Importing
Modules
1. Import Entire Module
import math
Example
print(math.factorial(5))
Output
120
2. Import Specific
Function
from math import sqrt
Example
print(sqrt(49))
Output
7.0
3. Import Module Using
Alias
An alias is an alternative short
name given to a module during import using the as keyword.
Example
import math as m print(m.sqrt(64))
Output
8.0
4. Import all functions from a module
Importing
all functions from a module means importing every function, variable, and
object from a module into the current program using the *
symbol.
from
module_name import *
Example:
from
math import *
print(sqrt(25))
print(factorial(5))
Output
5.0120
Important Point
After importing
with *, functions can be
used directly without writing the module name.
Example:
sqrt(25)
Instead of:
math.sqrt(25)
PACKAGE
Definition
A package is a collection of
related Python modules organized inside a folder or directory.
Packages help organize large Python
projects systematically.
Structure of a Package
calculator/│├── add.py├── subtract.py└── __init__.py
init.py
File
__init__.py
is a special file used to indicate that a directory should be treated as a
Python package.
Advantages of Packages
1. Groups
related modules together
2. Organizes
large projects
3. Improves
code management
4. Avoids
naming conflicts
5. Supports
hierarchical structure
Example of Package
Import
from calculator.add import add
Important Points About
Packages
·
Contains one or more modules
·
Usually includes __init__.py
·
Can contain sub-packages
·
Provides hierarchical organization
LIBRARY
Definition
A library is a collection of
modules and packages that provides reusable functionality for specific tasks.
Libraries help programmers perform
complex tasks easily and quickly.
Types of Libraries
1. Standard Library
A standard library is a collection
of built-in modules that are automatically available with Python installation.
Examples
·
math
·
random
·
os
·
datetime
2. External Library
An external library is a library
that is not included with Python by default and must be installed separately
using pip.
Examples
·
NumPy
·
Pandas
·
Matplotlib
·
TensorFlow
·
OpenCV
·
Django
Popular Python
Libraries
|
Library |
Main Use |
|
NumPy |
Numerical
Computing |
|
Pandas |
Data
Analysis |
|
Matplotlib |
Data
Visualization |
|
Requests |
API
Calls |
|
TensorFlow |
Machine
Learning |
|
OpenCV |
Computer
Vision |
|
Django |
Web
Development |
pip
Definition
pip is Python’s package manager
used to install, update, and remove external packages and libraries.
Common pip Commands
Install Package
pip install numpy
Uninstall Package
pip uninstall numpy
Show Installed Packages
pip list
Check pip Version
pip --version
Advantages of pip
1. Installs
external libraries easily
2. Updates
packages
3. Removes
unwanted packages
4. Manages
dependencies
5. Simplifies
package management
📊 Difference
Between Module, Package and Library
|
Feature |
Module |
Package |
Library |
|
Meaning |
Single
Python file |
Collection
of modules |
Collection
of packages and modules |
|
Size |
Smallest |
Medium |
Largest |
|
Form |
|
Folder/Directory |
Complete
collection |
|
Purpose |
Reuse
code |
Organize
modules |
Provide
complete functionality |
|
Example |
math.py |
numpy.random |
NumPy |
Common Exam Mistakes
❌ Confusing module and package
❌ Writing package as a single file
❌ Forgetting .py extension
in module definition
❌ Writing pip as a library instead of package manager
Final Summary
A module is a single Python file
containing reusable code. A package is a collection of related modules stored
inside a folder. A library is a collection of modules and packages that
provides reusable functionality for developers.