Hello, I have recently taken up python in my engineering.., python being one of the most trusted and preferred languages on desktop and being one of the high-level programming languages I now want to write a code for the Fibonacci series using modules and packages but I am not clear about the difference between modules and packages the same..? Can you elaborate on it? Thank you.
Share
Roshan
A Python package is a collection of Python modules and sub-packages. Modules in Python are simply Python files with a .py extension. The name of the module will be the name of the file. A module is a single python file that contains a set of functions, classes, or variables defined and implemented. Actually, it’s a good idea to split code into smaller modules. Python package is like a directory holding sub-packages and modules. There is a Python Package Index. (PyPI). We can also create our own packages.
Python module/package names should generally follow the following constraints:
Let us see how packages and modules of Python are interrelated:
we store our files in organized hierarchies. We don’t store them all in one location. Likewise, when our programs grow, we divide them into packages. In real-life projects, programs are much larger, package lets us hold similar modules in one place. Like a directory may contain subdirectories and files, a package may contain sub-packages and modules. But what distinguishes a package from a regular directory?
Well, a Python package must have an __init__.py file in the directory. You may leave it empty, or you may store the initialization code in it. But if your directory does not have an __init__.py file, it isn’t a package; it is just a directory with a bunch of Python scripts. Leaving __init__.py empty is indeed good practice.
Packages are namespaces that contain multiple packages and modules themselves. They are simply directories, but with a twist.
Each package and subpackage in Python is a directory that MUST contain a special file called __init__.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported. This will define what gets brought into the namespace with the import statement.
If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. We also must not forget to add the __init__.py file inside the foo directory.
To use the module bar, we can import it in two ways:
import foo.bar or from foo import bar
In the first method, we must use the foo prefix whenever we access the module bar. In the second method, we don’t, because we import the module to our module’s namespace.
Sample Package module structure:
An example python package
Python packages come in a variety of structures, but let’s create a simple demo one here that we can use in all the examples.
/src
/example_pkg
__init__.py
foo.py
bar.py
baz.py
setup.py
README.md
LICENSE
It is composed of three modules: foo.py, bar.py, and baz.py, each of which has a single function that prints the name of the module where the function resides.
This is some information about the python module and package. Hope this information might help you to get an idea of python modules and packages.
You may also read:
Python Program To Count The Number Of Vowels In A Sentence