Modules, Packages, Libraries & Frameworks
A lot of times, especially starting as a beginner or switching from one programming language to another, one might find certain terms confusing as they are sometimes defined differently by different programming languages.
Here’s how these terms; module, package, library, and framework are used in Python.
A Python module is simply a python file with a
.py extension. It contains variables, functions, or classes that can be referred to from another python file using an
import. This allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. Every module is independent unless linked explicitly, this means that any changes made in one module will not affect another. Python also has some built-in modules which are pre-defined in its library. Also, a user can create a module and import it wherever required, these are referred to as user-created modules. On executing the line
help ('modules') in Python IDE (Integrated Development Environment), you can see all the Python built-in modules.
import <modulename> # to import the entire module Or from <modulename> import <class/functionname> # imports a class or function from a module
A Python package refers to a directory of Python modules or a collection of python files in a folder. This directory usually contains an
__init__.py file by which the interpreter interprets it as a package. This comes in handy for organizing modules of one type in one place.
Test(Package) | __init__.py (Constructor) | details.py (Module) | marks.py (Module) | myDetails.py (Module)
You can then import the package;
#using import, you have access to the variables and methods inside the module import Test.marks or #use the dot notation to reference the variables and modules inside the module from Test.details import myfunction
A python library is a collection of python packages or modules. It contains related functionality of codes that allows you to perform many tasks without writing lots of code. The term standard library refers to the collection of exact syntax, token, and semantics of the Python programming language which comes bundled with the core Python distribution. The Python installer for Windows OS automatically adds the standard library and some additional libraries. You can also use packaging tools like easyinstall or pip to install other additional libraries. Find some examples of Python libraries here
A framework is a collection of Python libraries, It basically defines a skeleton where an application can define its own features to fill out the skeleton. In a framework, all the control flow is already there. The key difference between a library and a framework is in the “Inversion of Control”, commonly known as IoC. This simply means that when we call a method from a library, we are in control. But in a framework, the control is inverted i.e. the framework calls us. To put it together, we can think of a library as a certain function of an application and a framework as the skeleton of the application.