Similar questions have already been raised and answered in What's the difference between a module and a library in
Python? and What's the difference between a module and package in Python?
In short, only Package and Module actually have defined meanings in Python.
From the Python Glossary:
Module
An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing. See also package.
In other words, anything you import from is a module.
import sys
- sys
is the module
from math import floor
- math
is the module
from tensorflow.keras.layers import Dense, Flatten, Conv2D
- tensorflow.keras.layers
is the module.
- If I write two scripts
foo.py
and bar.py
and foo.py has the line import bar
, then bar
is a module.
- If you invoke a
foo.py
file with python -m foo.py
, then Python will load foo.py as a module, meaning that __name__ == "__main__"
will be false
when foo.py is loaded.
Package
A Python module which can contain submodules or recursively, subpackages. Technically, a package is a Python module with a __path__
attribute.
See also regular package and namespace package.
Regular Package
A traditional package, such as a directory containing an __init__.py
file.
Namespace Package -- Added in Python 3.3
A PEP 420 package which serves only as a container for subpackages. Namespace packages may have no physical representation, and specifically are not like a regular package because they have no __init__.py
file.
Packages are collections of related modules with a directory hierarchy. Anything you install using pip
or conda
or any other package manager is a package, although it may be a collection of modules or a single module. You can make your own local packages as well.
Library is not a special term defined in the Python glossary, but in programming a library is a general term for any reusable code. The built-in modules of Python are often referred to as the Standard Library, but its not a reserved term. Library is often used interchangeably with Package. It feels like some Python users tend to use Library
more often when referring to packages built in C such as scipy and numpy.
Framework is also not a defined term in the Python glossary, but generally refers to a collection of packages and modules which provide an abstraction layer for developers to create complex applications without having to reinvent the basic elements every time.
The distinction between library and framework is a bit nebulous, but we could generally say a library is targeting a specific functionality while a framework tries to provide everything needed for a full application, and may include several libraries.
Examples: