The terms module & package are defined differently by different programming languages. This caused some confusion to me while switching from another language to python. Here’s how these terms are used in the context of python.
A module is a single Python file that can be imported.
Let’s create a module with name module.py which has function hi().
def hi(): print("Hello world!")
Now, in another file my_script.py, we can use the import module shown below.
import module module.hi()
A package is made up of multiple Python files (or modules), and can even include libraries written in C or C++.
Instead of being a single file, it is an entire folder structure which might look like this:
from package.hello import print_hello from package.world import print_world
def print_hello(): print(“Hello”)
def print_world(): print("World!")
All Python packages must contain a __init__.py file. When you import a package into your file, the __init__.py script will run which gives you access to the all of the functions in the package. In this case, it allows you to use the package.hello and package.world functions.