A python package is a collection of modules. Modules that are related to each other are mainly put in the same package. When a module from an external package is required in a program, that package can be imported and its modules can be put to use.
Any Python file, whose name is the module’s name with the .py extension, is a module.
A package is a directory of Python modules that contains an additional __init__.py
file, which distinguishes a package from a directory that is supposed to contain multiple Python scripts. Packages can be nested to multiple depths if each corresponding directory contains its own __init__.py
file.
When you import a module or a package, the object created by Python is always of type module.
When you import a package, only the methods and the classes in the
__init__.py
file of that package are directly visible.
For example, let’s take the datetime
module, which has a submodule called date
. When datetime
is imported, it’ll result in an error, as shown below:
import datetimedate.today()
The correct way to use the date
module is shown below:
from datetime import dateprint date.today()