📦 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 module1

Note: In modern Python (3.3+), __init__.py is 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 operations
  • Pandas – data analysis
  • Requests – 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 framework
  • Flask – micro web framework
  • PyTorch – 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 system
  • math – mathematical functions
  • datetime – 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

TermDescriptionExample
ModuleA single .py filemath.py
PackageA folder of modules (with __init__.py)mypackage/
LibraryCollection of packages/modulesNumPy, Pandas
FrameworkStructured toolkit for app developmentDjango, Flask
Standard LibraryBuilt-in modules with Pythonos, random, json
Virtual EnvIsolated environment for managing packagesvenv, virtualenv