Modules & Packages

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Modules & Packages

Modular Programming
Modular programming refers to the process of breaking a large, unwieldy programming
task into separate, smaller, more manageable subtasks or modules. Individual modules
can then be cobbled together like building blocks to create a larger application.There are
several advantages to modularizing code in a large application:
● Simplicity: Rather than focusing on the entire problem at hand, a module typically
focuses on one relatively small portion of the problem.
● Maintainability: Modules are typically designed so that they enforce logical
boundaries between different problem domains.
● Reusability: Functionality defined in a single module can be easily reused (through
an appropriately defined interface) by other parts of the application.
● Scoping: Modules typically define a separate namespace, which helps avoid
collisions between identifiers in different areas of a program.
Python Module
A python module can be defined as a python program file which contains a
python code including python functions, class, or variables. In other words, we
can say that our python code file saved with the extension (.py) is treated as the
module. We may have a runnable code inside the python module. Modules in
Python provides us the flexibility to organize the code in a logical way
Loading the module in our python code
Several syntaxes can be used when importing:
❖ import importable
❖ import importable1, importable2, ..., importableN
❖ import importable as preferred_name
❖ Make the imported objects (variables, functions, data types, or modules)
directly accessible.
❖ from …import syntax to import lots of objects.
Loading the module in our python code
Here are some other import syntaxes:
❖ from importable import object as preferred_name
❖ from importable import object1, object2, ..., objectN
❖ from importable import (object1, object2, object3, object4, object5, object6,
..., objectN)
❖ from importable import *
Python import statement
We can import a module using the import statement and access the definitions
inside it using the dot operator.

import math
print(“The value of pi is”,math.pi)

#import with renaming


import math as m
print(“The value of pi is”,m.pi)
Python from….import statement
● We can import specific names from a module without importing the module
as a whole.
#import only pi from math module
from math import pi
print(“The value of pi is”,pi)

● Import all modules

from math import *


print(“The value of pi is”,pi)
Creating a module
Suppose you have created a file
called mod.py. Several objects are
defined in mod.py:
● s (a string)
● a (a list)
● foo() (a function)
● Foo (a class)
Importing the module
The module search path
When the interpreter executes import statement, it searches for
mod.py in a list of directories assembled from the following
sources:

● The directory from which the input script was run or the current directory
if the interpreter is being run interactively
● The list of directories contained in the PYTHONPATH environment
variable, if it is set. (The format for PYTHONPATH is OS-dependent but
should mimic the PATH environment variable.)
● An installation-dependent list of directories configured at the time Python
is installed
Module contents can be imported from within a function definition. In that case,
the import does not occur until the function is called. However, Python 3 does
not allow the import * syntax from within a function
Reloading a module
For reasons of efficiency, a module is only loaded once per interpreter session.
That is fine for function and class definitions, which typically make up the bulk
of a module’s contents. But a module can contain executable statements as well,
usually for initialization. Be aware that these statements will only be executed the
first time a module is imported.
The print() statement is not executed on
subsequent imports. (Once the assignment
is made, it sticks.)
If you make a change to a module and need to reload it, you need to either
restart the interpreter or use a function called reload() from module
importlib
Packages
Suppose you have developed a very large application that includes many
modules. As the number of modules grows, it becomes difficult to keep track of
them all if they are dumped into one location. This is particularly so if they have
similar names or functionality. You might wish for a means of grouping and
organizing them.
First of all, we need a directory. The name of this directory will be the name of
the package, which we want to create.
● We will call our package "pkg". This directory needs to contain a file with
the name __init__.py. This file can be empty, or it can contain valid Python
code.
● This code will be executed when a package is imported, so it can be used to
initialize a package.
● We create two simple files mod1.py and mod2.py just for the sake of filling
the package with modules.
Packages example
import packages
Package initialization

Now when the package is imported, the global list A is initialized:

You might also like