When the Python coder has to deal with real world projects, they need to have knowledge about the modules and packages. As I have mentioned in my previous articles about the Python computer programming language, anything ending in a .py is considered to be a module.
For example the following is a Python module.
The Python coder can easily make use of the code being stored in a module by importing it with the help of the Python’s builtin import statement.
Launch a new Python interactive console and try to import the module we just created with the help of the following command.
If you have launched the interactive Python console inside a directory in which the module you’re trying to import is not present, the following traceback will come up.
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named testme
If the import is successfully completed, you can easily make use of the code being found in the module by making use of the syntax show below.
Let’s try to access some of the code being stored inside the Python module called testme.
import testme testme.x
Once I managed to execute the above piece of Python code in my interactive shell, I got the following output.
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'x'
The above error called AttributeError was being thrown because there is no attribute x defined in the module testme.
Open the module testme.py in editing mode and define the attribute which is being shown below.
x = 4
Then save the module, restart the interactive session and try to access the attribute x.
import testme testme.x
If everything has been coded correctly, the above code should return the output being shown below.
On the other hand, a package in Python has multiple modules. Its content can be utilized with the same methods as the ones we used for the simple module.
To declare a package in the Python computer programming language, one must make use of the following module.
The coder can easily create a package in the Python computer programming language by making use of the following tree.
ffmpeg_pkg __init__.py base.py
In the above tree, the name of the Python package is ffmpeg_pkg. As far as my knowledge goes, the __init__.py module tells Python that the directory is a package.
Let’s materialize the above tree in a Python package. Once you have managed to do that, you can easily access the entire package by importing it in your interactive sessions with the help of the Python’s builtin import statement like shown below.
Then we can easily access each one of the modules being found inside the package by making use of the following syntax.
Let’s try to access the module base which is being found inside the package ffmpeg_pkg by making use of the following piece of Python code.
The above call will result in the following error.
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'base'
We need to add the following line to the __init__.py file of our package for the above code to work.
from . import base
Let’s restart the Python’s interactive console and give it another try.
import ffmpeg_pkg ffmpeg_pkg.base
Once I executed the above piece of Python code, I got the following output.
<module 'ffmpeg_pkg.base' from 'ffmpeg_pkg/base.pyc'>
A Python package is perfect when it comes to grouping modules together. Every package in the Python computer programming language should have the __init__.py file.