Hi again! In previous tutorials I described How to Setup Visual Studio Code to debug Python and also PYTHONPATH. In this tutorial I will talk about Modules and Packages. We all know that, one cannot put all his/her python files in one big directory. It will be impractical in terms of book keeping ass well as will make searching a lot difficult. Also, there will not be any logical segregation of functionalities, classes etc. So, we all need to divide our code base into logical chunks. This gives rise to Multi-directory project.
Before we get to the project level let’s briefly understand the meanings of Modules and Package. Module is practically any python file. It can contain class definitions, functions, some data or a runnable code. So in short any python file can be called a Module. A Module and/or a method in a module can be directly referenced in another Module.
Example of Module
While Package documentation according to official is way of structuring Python’s module namespace by using “dotted module names”. In simple words packages are bundles of Python modules which can be called by their directory names. A Package MUST have __init__.py module. Just like a directory can have a sub-directory packages can have sub-packages A typical Package is structured as following..
ParentName/ <Parent Package> __init__.py <Initialize Package> module1.py <Modules within the Package> module2.py <Modules within the Package> . . . moduleN.py <Modules within the Package> Sub-PackageName/ <Sub-Package> __init__.py <Initialize Sub-package> subModule1.py <Modules within Sub-Package> subModule2.py
Example of Package
Please do let me know if you have any questions. You can post them in the comments section.