📦 Module, Package, Library, Framework in Python
🔹 1. Module
A module is a single Python file (.py) that contains code—functions, classes, variables, etc.
# my_module.py
def greet(name):
return f"Hello, {name}!"You can import this module:
import my_module
print(my_module.greet("Alice"))🔹 2. Package
A package is a collection of modules grouped together in a directory with an __init__.py file (can be empty).
my_package/
│
├── __init__.py
├── module1.py
└── module2.py
You can import like this:
from my_package import module1Note: In modern Python (3.3+),
__init__.pyis optional, but still commonly used.
🔹 3. Library
A library is a broader term—it refers to a collection of packages and modules that offer related functionality.
Examples:
NumPy– numerical operationsPandas– data analysisRequests– HTTP requests
So a library may consist of many packages, each with its own modules.
🔹 4. Framework
A framework is a more structured collection of libraries designed to help build applications, especially web or GUI ones.
Examples:
Django– web frameworkFlask– micro web frameworkPyTorch– deep learning framework
Frameworks are opinionated—they define how you should structure your app.
🔹 5. Standard Library
This is the built-in set of modules that comes with Python. You don’t need to install them.
Examples:
os– interacting with the operating systemmath– mathematical functionsdatetime– working with dates and times
🔹 6. Virtual Environment
An isolated environment where Python packages are installed. Prevents conflicts between different projects.
python -m venv myenv
source myenv/bin/activate # or myenv\Scripts\activate on Windows🧾 Summary Table
| Term | Description | Example |
|---|---|---|
| Module | A single .py file | math.py |
| Package | A folder of modules (with __init__.py) | mypackage/ |
| Library | Collection of packages/modules | NumPy, Pandas |
| Framework | Structured toolkit for app development | Django, Flask |
| Standard Library | Built-in modules with Python | os, random, json |
| Virtual Env | Isolated environment for managing packages | venv, virtualenv |