Problem Solving using Python
(ITFC0101)
Modules and Packages
Dr. Naveen Kumar Gupta
Assistant Professor
Department of Information Technology
Dr B R Ambedkar National Institute of Technology Jalandhar
1
Python Module
• Module: A python file containing a set of definitions
• Definitions may be: functions, classes, and variables
• Also include runnable code
Why module:
• Grouping related code into a module makes the code easier to understand
and use
• Logically organized code
2
Create a module
• Create a file with extension (.py) def greeting(name):
• Write definition of function, print("Hello, " + name)
classes and variables
• Save the file
Save this file as mymodule.py
• Live demo
3
Use a module
Use import statement and apply dot (.) symbol
import mymodule
mymodule.greeting(“NITJ")
4
Aliasing/renaming a module
Use import statement
import mymodule as mm
mm.greeting(“NITJ")
5
Variables in module
myModule.py Test.py
t1 = 67656 Import myModule as mm
person1 = { print ("Variable", mm.t1)
"name": "John",
"age": 36, a = mm.person1["age"]
"country": "Norway" print(a)
}
6
Import specific attributes of module
‘from’ keyword: No need to write module name with attribute
from Calculator import division, mod, subtraction
print("Division from calculator module: ", division(a, b))
7
Import all attributes of module
No need to write module name with attribute
from Calculator import *
print("Division from calculator module: ", division(a, b))
8
Built-in Modules
Modules defined by python library
• E.g.:
• math (functions: factorial, ceil, floor, reminder, sin, cos, tan etc.)
• array (append, count, insert, reverse, remove etc)
• string
• datetime
• calendar 9
https://docs.python.org/3/py-modindex.htm
l
Locating python modules
After encountering the import statement:
• Whenever a module is imported in Python:
• Interpreter looks for several locations
• First, it will check for the built-in module, if not found
• Second, it looks for a list of directories defined in the sys.path
10
Locating python modules
After encountering the import statement:
If module not available in the built-in modules:
• First, searches for module in current directory which contains script
• Then, searches each directory in the shell variable PYTHONPATH
• The PYTHONPATH is an environment variable, consisting of a list of
directories
• Import module from different directory using sys module
• sys.path.insert(0, ‘<PATH>’) sys.path.append(‘<PATH>’)
• If that also fails python checks the installation-dependent list of directories
configured at the time Python is installed
11
Packages
12
Packages
• Python modules: contain several classes, functions, variables, etc.
• Python packages: contain several modules
• Package: A folder that contains various modules as files
13
14
Creating Package
• Create a folder named mypack
• Inside this folder:
• Create an empty Python file: __init__.py
• Create two modules module1 and module2
15
Creating Package
• Create a folder named mypack
• Inside this folder:
• Create an empty Python file: __init__.py
• Create two modules (.py files) module1 and module2
module1.py
module2.py def mod11():
def mod21(a, b): print(“Module 1 of mypack")
print(“Module 2 of mypack")
return (a+b)
16
init.py
• Helps the Python interpreter recognize the folder as a package
• Specifies the resources to be imported from the modules
• If the __init__.py is empty 🡪 functions of the modules will be imported
• Also specify the functions from each module to be made available**
17
init.py
This __init__.py will only allow the
• mod11 function from the module1
• mod21 functions from the module2
to be imported
__init__.py
from .module1 import mod11
from .module2 import mod21
18
Import Modules from a Package
(i) Importing package and accessing module with dot (.) operator
• Syntax: from package_name import module_name
from mypack import module1
from mypack import module2
module1.mod11()
res = module2.mod21(1, 2)
print(res)
19
Import Modules from a Package
(ii) Directly importing package using the dot(.) operator
• Syntax: import package_name.module_name
from mypack.module1 import mod11
from mypack.module2 import mod21
mod11()
res = mod21(1, 2)
print(res)
20
Thank You!
21