β Python Packages
A package in Python is a way of organizing related modules into a directory (folder) structure.
Packages make it easier to structure large programs and avoid name conflicts.
π Package = A folder containing multiple Python modules + an __init__.py file
In simple words:
- Module = A single Python file (.py)
- Package = A folder containing modules
β Why Do We Need Packages?
β To group related modules
β For better code organization
β To avoid naming conflicts
β To support large-scale project development
β To reuse code across multiple projects
β To make distribution easier
β Structure of a Package
A typical package structure looks like this:
myproject/
mathpkg/
__init__.py
addition.py
subtraction.py
multiply.py
main.py
π What is __init__.py?
- Special file that identifies the folder as a Python package
- Can be empty
- May contain initialization code
- Runs when the package is imported
Before Python 3.3, it was mandatory; now it is optional but still recommended.
β Creating a Python Package (Step-by-Step)
β Step 1: Create a Folder (Package)
Create a folder named myutils
myutils/
β Step 2: Add __init__.py file
myutils/
__init__.py # can be empty
β Step 3: Add Modules Inside the Package
Example modules:
addition.py
def add(a, b):
return a + b
subtraction.py
def sub(a, b):
return a - b
β Step 4: Use the Package in Another Program
from myutils.addition import add
from myutils.subtraction import sub
print(add(10, 20))
print(sub(50, 15))
β Importing Packages
There are many ways to import from packages:
1. Import Entire Package
import myutils
2. Import Module from Package
import myutils.addition
3. Import Function Directly
from myutils.addition import add
4. Import All Modules
from myutils import *
(works only if __init__.py contains __all__ list)
Example:
__all__ = ["addition", "subtraction"]
β Built-in Python Packages
Many Python libraries come packaged:
| Package | Description |
|---|---|
os | Operating system interface |
sys | System parameters |
json | JSON handling |
numpy | Scientific computing |
pandas | Data analysis |
matplotlib | Plotting |
tkinter | GUI toolkit |
collections | Specialized data structures |
β Installing Third-Party Packages
Use pip, Pythonβs package manager:
pip install numpy
pip install pandas
To uninstall:
pip uninstall numpy
To list installed packages:
pip list
β Difference Between Module and Package (Exam Point)
| Module | Package |
|---|---|
Single .py file | Folder containing modules |
| Small code unit | Large code grouping |
| No directory required | Requires directory structure |
| Cannot contain submodules | Can contain submodules and subpackages |
Example: math.py | Example: numpy package |
β Advantages of Packages
β Organizes large projects
β Avoids name conflicts
β Promotes code reusability
β Supports modular programming
β Allows distribution of libraries
β Makes maintenance easier
β Exam-Ready Short Answer
A Python package is a collection of related modules organized in a directory containing an __init__.py file. Packages help structure large programs, avoid naming conflicts, and promote code reusability. Example: NumPy, Pandas, Matplotlib, Requests.
