Python Libraries4
Python Libraries4
syllabus
2022-23
Chapter 4
Python
Libraries
Libr- Libr-
ary1 ary2
Framework=multiple library
mod-
ule1
mod-
ule2
Library=multiple packages
Package=multiple module
Module=multiple function/class
Visit : python.mykvs.in for regular updates
Using
Python Libraries
Following terms must be clear while developing any python
project/program.
1. Module
2. Package
3. Library
4. Framework
1. Using Module -It is a file which contains python functions/global
variables/clases etc. It is just .py file which has python executable code /
statement.For example: Let’s create a file usermodule.py
def hello_message(user_name):
return “Hello " + name
Now we can import usermodule.py module either in python interpreter
or other py file.
import usermodule
print usermodule.hello_message(“India")
Visit : python.mykvs.in for regular updates
Using
Python Libraries
How to import modules in Python?
Python module can be accessed in any of following way.
1. Python import statement
import math
print(“2 to the power 3 is ", math.pow(2,3))
Just similar to math ,user defined module can be accessed using import
statement
2. Import with renaming
import math as mt
print(“2 to the power 3 is ", mt.pow(2,3))
3. Python from...import statement
from math import pow
print(“2 to the power 3 is ", pow(2,3))
4. Import all names
from math import *
print(“2 to the power 3 is ", pow(2,3))
Visit : python.mykvs.in for regular updates
Using
Python Libraries
2. Using Package - It is namespace that contains multiple package or modules. It is a
directory which contains a special file __ init __.py
Let’s create a directory geometry. Now this package contains multiple packages /
modules to handle user related requests.
geometry/ # top level package
__ init __.py