Here we will discuss the highest level program organization unit, which helps programmers to pack and organize program code and data for reusable purposes.
What is Modular Programming?
A module provides a simple way to organize program components into a system; grouping similar forms of code into a module makes code easier to understand. It is also referred to as a program object that binds data and can be used as a reference. In other words, it is a file that contains a Python program. Programmers use the 'import' statement to get access to the names in a module's global scope. We will learn about it a bit later. Python's module ultimately allows programmers to link individual files to a larger program system.
Roles of Python Module
- Code Reuse: Modules let programmers save source code in files permanently & hence the codes in module files are persistent. Module codes can rerun as many times as required.
- System Namespace Partitioning: In Python, the highest-level organizational unit of code is the Modules. Although they are just packages with names - these packages are self-contained. Like that of a local scope of functions, modules help to avoid name clashes across the program. So in a module, all the objects and codes are implicitly enclosed or bounded. Hence, modules are the natural programming tools used to group system components.
- Shared Service or data Implementation: From the operational point of view, modules are also used to implement components that are shared across systems. For example, if a programmer needs to provide a global object which will be used by more than one file or function blocks, then the code can be used as a module that can be imported by several files.
Programmers can use any Python source code (i.e., .py files) as a module by implementing an 'import' statement in another Python program or Python file.
Let take a scenario, a file name 'g.py' be chosen as the top-level file; it will be a simple text file of statements and will be executed from top to bottom as the interpreters do. The modules are - 'a.py' and 'b.py'. So to use the codes of a.py and b.py in the g.py program, we have to first define the codes as functions in both modules & then import the two Python codes using 'import' statement.
The code will be:
def karl(text)#a.py File print(text, 'karl')
Now, the programmer wants to use the codes of a.py into the g.py file.
So the program will be:
import a#g.py file a.karl ('myself')#Prints "myself karl"
The figure below shows its working:
So the syntax for import statement is:
import module_name1 [, module_name2 [module_nameN]]
The module can be imported only when the interpreter encounters an import statement & if the module is present in the search path, i.e., the directories that the interpreter searches before importing module.
Standard Library Modules in Python
Python comes with a vast collection of regularly used utility modules called standard library which contains a collection of over two-hundred modules along with platform-independent support system for common programming tasks such as network, internet scripting, text pattern matching, object persistence, GUI (Graphical User Interface) programs and much more.
Though these tools are not the part of Python programming language, programmers can use them by importing the appropriate modules based on the needs. Since they are standard library modules, therefore they will be available & will work portably on most platforms on which the Python will be run.
How does import Work?
Because imports are an essential part of the Python program structure, let's go to the inside of its working technique in brief. From the C-programmers viewpoint - import operation is like #include of C Language, but in reality, 'import' is not the just textual insertion of one file into another. These are runtime operational statements that perform three distinct steps. These are:
- Find the module's file from the search-path
- Compile it to byte-code (if needed)
- To build the object, run the module's code
It has to be kept in mind that the above three steps are performed only the first time the module is imported during program execution. If the program is run later importing the previous modules, bypass all these three steps and simply fetch the already loaded module-object in memory.