Python code is organised in layers, from a single file up to large frameworks. The terms are often mixed up, so this section defines each one precisely and shows how to import them.
The four terms
Term
What it is
Example
Module
A single .py file containing definitions (functions, classes, variables) and statements
math, or your own geometry.py
Package
A directory of modules (and sub-packages), imported with dotted names
xml, email, game.sound
Library
An informal term for reusable code distributed for others to use; it may be one module or many packages
NumPy, Requests, the standard library
Framework
A library that provides the overall structure of an application and calls your code at the right moments
Django, Flask, PyTorch’s training loop helpers
Creating and using a module
Any .py file is a module. Its name is the file name without .py.
Creating a package
A package is a folder. Placing an __init__.py file in it makes it a regular package; the file can be empty, or it can run set-up code and define __all__, the list of names that from package import * brings in. Since Python 3.3, a folder without __init__.py still works as a namespace package, but regular packages remain the normal choice for your own code.
Module = one .py file; package = folder of modules; library and framework are broader, informal terms.
import m, from m import x, and import m as alias are the three common import forms.
__init__.py marks a regular package and can define __all__ for import *.
A framework calls your code; you call a library.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain, with one example each, the difference between a library and a framework.
Show worked answer
With a library, your program is in control and calls the library’s functions when it needs them; for example, your script calls pandas.read_csv("sales.csv") to load data. With a framework, the framework is in control and calls your code; for example, in Django you write a view function, and Django calls it whenever a matching web request arrives. This reversal is called inversion of control.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/introduction-to-python/modules-packages-libraries-and-frameworks/ to read this page.