In Python, we have modules for files and packages for directories. It is a hierarchical file directory structure that defines a single Python application environment that consists of modules and sub-packages and sub-sub-packages, and so on.
Packages are a way of structuring many packages and modules which helps in a well-organized hierarchy of data set, making the directories and modules easy to access.
A package can contain one or more relevant modules. It is actually a folder containing one or more module files. It is basically a directory with Python files and a file with the name __init__
.py. This means that every directory inside of the Python path, which contains a file named __init__
.py, will be treated as a package by Python. It's possible to put several modules into a Package.
Importing module from a package:
In Python, We can import packages using import keyword .
Example:
import math
Another way of importing just the required function (or class or variable) from a module within a package would be as follows:
Example:
from math import pi
While importing packages, Python looks in the list of directories defined in sys.path
, similar as for module search path.