Python Libraries
• Frequently used modules are generally
e r
known as libraries which contain code for
general purpose.
i n n
a r
• These libraries are the collection of methods,
classes which can be used easily.
components. -
x c
• Python program is made using 3 different
N e
– Library or package
– Module
– Functions/sub-modules
Neha Tyagi, KV No-5 Jaipur
Relation Between Python Libraries, Module
and Package
• A module is a file containing
e r
python definition,
variables, classes and statements.
functions,
i n n
The extension of this file is “.py”.
• While Python package,
a r is
directory(folder)
modules.
of
x c python
N e
• A library is collection of many
packages in python. Generally
there is no difference between
python package and python
library. Neha Tyagi, KV No-5 Jaipur
Module in Python
• A module is a file containing python definition,
e r
functions, variables, classes and statements. The
extension of this file is “.py”.
• The name of module is the name of .py file e.g. in
i n n
a r
[Link] the module name is math. And _name_
variable is used to store this module.
• Advantages–
x c
– Its biggest advantage is that we can import its functionality in
any program and use it.
N e
– Reusability is one of the biggest advantages.
– It helps in logically organization of Python code.
– Programming becomes very easy when we use the collection of
same types of codes.
– Categorization : same attributes can be stored in one module.
Neha Tyagi, KV No-5 Jaipur
Processing of import <module> command
occur -
e r
• When you run the import <module> command then the following things
n
1. Imported module’s code gets executed after interpretation.
2. All the programs and variables of imported module are present in the
program.
r i n
3. A namespace setup is created for the imported module in the name of
module.
command
c a
Processing of from <module> import <object>
e x
• When you run the from<module> import <object> command then the
following things occur -
1. Imported module’s code gets executed after interpretation.
2. Only asked programs and variables of imported module are present in
N
the program.
3. No namespace is created. Import objects of module are attached to
current namespace.
Neha Tyagi, KV No-5 Jaipur
How to Make modules in Python?
• To make a module in python you have to make a .py file
which has a name. Suppose we make a file “[Link]”.
e r
• Then we write different functions of area within it.
i n n
• And we put it within same folder where our program is stored.
a r This is module.
x c
N e Output
We used the module in our program
by using import keyword. To use the
members of module we use (.) as
shown in the example.
Neha Tyagi, KV No-5 Jaipur
How to Make modules in Python?
• Using from <module> import <function_name>
e r
i n n
a r This is module.
x c
N eOutput
By using “from shape import AreaCircle
“only AreaCircle function is imported
and to call this there is no need to use
(.) operator.
Neha Tyagi, KV No-5 Jaipur
Namespaces in Python
e r
• We imported a module in to a program which was referred
as a namespace.
i n n
• To define a Namespace it is necessary to define its name.
• Python name is a kind of identifier which we use to access
object.
a r
any python object. And in Python each and everything is an
• Namespaces is used to separate the different sections of a
program.
x c
• Python has 3 types of namespaces -
– Global
– Local
N e
– Built-in
Neha Tyagi, KV No-5 Jaipur
Namespaces in Python. .
e r
• This is just like variable scope in Python.
• When we start interpreter Then a namespace is
i n n
a r
created in which print( ) and id( ) etc. are already
exist. This holds all the built-in name.
x c
• Each module creates its own global namespace.
• When we call a function then a local python
N e
namespace is created where all the names of
functions are exist.
Neha Tyagi, KV No-5 Jaipur
Resolving the scope of a name
• For every name reference python always follows name
resolution rule when you access varaibles from any
e r
program or function.
• This is known as LEGB rule . Following works are
done when this rule is followed -
i n n
a
available then it will be used. r
– First variable is checked in Local Environment, and if it is
c
– Then variable is checked in Enclosing Environment, and if it is
available then it will be used.
x
– Then variable is checked in Global Environment, and if it is
N e
available then it will be used.
– Then variable is checked in Built-in Environment, and if it is
available then it will be used.
– Other wise Python will generate an error.
Neha Tyagi, KV No-5 Jaipur
Module Aliasing in Python
• When you import module in Python program then we have to
use the following syntax to make alia name of module -
e r
import <ModuleName> as <AliaName>
i n n
• Then we use (.) operator to use the members of the module
a r
with alia name of module. Example is given below -
This is module.
x c Creating and Using the Alia Name of
e
Module with in a program.
N Output
Neha Tyagi, KV No-5 Jaipur
Module Aliasing in Python
• When you import member of any module with alia name then
you have to use the following syntax -
e r
from <ModuleName> import <MemberName> as <AliaName>
i n n This is module.
a r Creating and using of alia name of
c
member of module.
e x
N
Output
Neha Tyagi, KV No-5 Jaipur
PACKAGE / LIBRARY
e
• Python Package is the collection of same type of modules.
• You can use built in package as well as your own package.
r
that directory.
i n n
• Python Package is a simple directory. Modules are stored in
a r
• Package or library are generally same.
• Steps to make a package is as follows – (geometry)
c
1. We create a folder (directory) named geometry which will contain
two files [Link] and [Link]
x
2. In the same directory we will put another file named “__init__.py”
e
3. __init__.py is necessary because this is the file which tells Python
that directory is package. And this file initializes the package
N
4. Then we import the package and use the content.
Neha Tyagi, KV No-5 Jaipur
Making PACKAGE / LIBRARY
e r
This is the folder
i n n which will be our
package.
r
This is __init__.py file
which is made directly
a
by saving from python
IDLE
Here the Package is used.
x c These are the
modules which are
the members of the
N e package.
Output
Neha Tyagi, KV No-5 Jaipur
Locating The Module
• When we import a module then interpreter of
e r
python searches the module in the following
sequence –
i n n
1. First current directory
a r
2. If module not found then it will be searched in
x c
shell variable PYTHONPAT.
3. If not found anywhere then python searches in
N e
default. Default path is the path where Python
is installed.
Neha Tyagi, KV No-5 Jaipur
Standard Python Libraries
• In last chapter we discussed about math and string
module. Same way we will discuss two more
e r
module.
i n n
important libraries. - datetime library या datetime
tow important classes -
a r
• Python supports yyyy-mm-dd format for date. It has
– date class
x c – time class
•
•
•
•
N e
today ( )
year( )
month ( )
day ( )
•
•
•
•
now ( )
hour ( )
minute ( )
second ( )
Neha Tyagi, KV No-5 Jaipur
Standard Python Libraries
Date Class Time Class
e r
i n n
a r
x c
N e Passing arguments in Time Class.
Neha Tyagi, KV No-5 Jaipur