0% found this document useful (0 votes)
8 views5 pages

module, package, library

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

module, package, library

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Thi

276 Chapter9 edeveloping it.


echnkyin nakes
heed of conpletethe project. We
without he less tine to
programmers
easy and takes
developent
theseconccptsnow
So far, we developed several Pythonprograms. These prograns can be Called te
whatis a n
Modules
other
programs, So,
Python imported into other programs,
useditbymay containany
Python program,
oodule?A modse
be stufflike classes, objects, functions, Cmstarts,
theycan be program that can
but a
Python io, time
several
like sys,
built-in modules create our oWn modules
also
math, pickle,
we have modules, we can (or
In Python,
these
2simplefunctions add()and
like now develop a module Python program)
sub)toperform addition and subtraction,,respectiney
etc, Just need them. Let us
whenever we

functions,
with
Psogram Program to create a module
Program 44: A contains 2
functions
module arith.py
# This
Thismodule name 1s
# add(x, y):
def
ZXcam of tWO nos 2)
print('s
def sub(x, y):
Z= X-y
return Z
function is displaying the
oftwo numbers;
In the preceding observe
whereas,
code, that the add) result
of Sa
the sub() function is returning the result of s1ubtracton.
call
by the name 'arith.py'. Now we can 'arith.py' as a module
this code in a file program, e.g. 'use.py'. For this purpose, We should
arith.py'
be moduleintothe use.py' program. This can be done by writing import statet
used in another
inside the'use.py' program. Thefollowing are different ways of writing import statement

O import
When arith
we write the module name in the import statement as shown in the preceting
module name before every Python object in our
code, PVM add this
will means
That if we have the add() function in our program, PVM wil mak.ie pogram,
arith.add) internally.
use.py. Hence, we should call the add() function in our program
arith.add() or arith.sub().
O import arith as ar
the 'arith' module. When an alias n
Here, 'ar' is another name or alias name for
in the progre
used like this, PVM will add the alias name before the Python object
ar.subl).
ar.add) or
Hence, we can call the function using the alias name as
Pomarithimport
represents all' In this case, PVM
does not bring the
Instead, it brings all the module narme 'arith irit
HereprOgram,
program.
our That means the
names add and names
sub are inside the arith' module int r
can
refer tothose functions
directly available
as add) or directly to our
Hence, we
import add,
arith spccifically
sub sub) progam
fron mentioning the
are names of the
from the functions
O add and
we
Here, PVM brings the nanes sub add and sub. n
case, Therefore, we can call them
directly arith
as add() or module this
program.
In Program45, we have shown how toimport the 'arith.py' module directly into
sub). and call the add) and
our
available in that module.
functions
sub)

:A Program toimport and use the arith.py module.


Program45:
use.py program
importisarith module using one of the above 4 ways
#this
import arith

call
functions of the module
the(10,
arith.add
# 22)
result = arith.ssub(10, 22)
print('Result of subtraction=, result)

Output:
C:\> python use.p
nos=
twosubtraction=
Sum of of
Result -12

Suppose, in the preceding program, we wrote the import statement as:


from arith import *
Then, we need not write the module name before the functions while calling them. The
looks like this:
same Program 45
arith.py module - version 2
# using the import
from arith #
module
# call the functions of the
add (10, 22)
22)
result = sub(10, subtraction=,
print(' Result of result)
Since we are importing the arith.py' prOgram into the use.py' program, we call the
"arith.py program as a module. Once imported, the contents of the arith module can be
used in the use.py program. In this manner, the arith module can be reused not onlv in
the use.py program but also in any other program. Thus, modules can be understood as
reusable Python programs. Due to their reusable nature, they make development of the
software easy.
278 Chapter9
co
directory that contains some modules. How
Packages folder or folder is
the question. To
init_py'differentiate pNM
paisctkionggeyihesrida
wepackage
need toiscreate
a
from
package
an empty
a
ile by the name he tolder. The
inside the package
A folder mypack with a module
normal
shows a
figure add) and
followingfunctions
package
sub) and we have to use then in another
Figure 9.8:
in it. Th "arith.py
package. See
contains
outsideofthe
whichis mypack
init_py 0 KB use.py

arith.py call the


functions
def add(x,y):
def sub(x.y):

packagefrom another program.


Figure 9.8: Using the
First of al, we should create a folder by the name mypack'. For this purpose, right cicy
folder by
Folder. Then it will create a new the name
’ New ’ New folde
the mouse mypack'
The nextitstep
Rename as is to create initpy' file as an empty file inside mypack' folder. First, 20
tothe mypackfolder by double clicking on it. Then right click the mouse ’ New ’Ten
Text Do
Notepad file by the name New
click file towill
onthis This
Document.
open a
open it. Then File > Save As ’ Type the file name in double quotes
Figure 9.9:
Document.txt. Doubje
init.py". Please observe
*

Deleie Resame Nes Seve As


eye tolses
ta Softasres (E)> py > ypack

res E py mypeck Orasrix New foltr

30 Obiects
New Tect Document.btt Desitop New TextDocunnent.bt
Documents

Downlcads

Pictures
Videcs

Local Disk (C)


Personst {D
Soltwsres (E:)

File nameinitpy
Save as type: Text Decurnents bt)

Cance
A Hide folders Encoding UTF-&

Figure 9.9: Creating an empty init-py file in mypack


a new ile by
the name
arith.py' program (See initpy with (0 KB size Functv 19
Program 44)
the

into this created in the my p


with
Store
reated as a
package
arith.py as a
nodule.
ter
mpackie

mypack. We can
use
this
mypack mvpack folder. Nor
of
out the package. We can write
the package in any
package, as shown in Program 46. use.Py program toprogram like
stored
mypDack use.py that
imnport the modul
e
the e from
Program showing how to use a
mypackmodulpackage
e belonging to a
46:A
Program
the arith.py module of
import mypack.arith
#using

functions of the module


package.
call the
# ith. add (10, 22)
mypack.ari
result =mypack.arith,sub(10, 22)
print('Result of subtraction=
result)
Output:
C:\>python use.py
noS= 32
twosubtraction=
of of
Sum
Result -12
the import statement in the
Please
observe
O importmypack.arith beginning of this program.
represents that mypack is the package (or folder) in
It module is being imported. When we which the arith
This arith and module name before the import ike this, we module is found.
package name functions or classes need to add the
as mypack.arith.add() that are
the module
the other ways of writing the
defined inside
following are
The
mypack.arith as ar
import statement:
O import
Then refer to the functions as: ar.add) or ar.sub)
O from mypack.arith import *
Dfrom mypack.arith import add, sub
ibe above two cases, we can directly call
the functions as add() or sub)
Libraries
We can extend the concept of
'software library', often simply packages
to torm libraries. A group of
called a ibrary'. A library packages is called a
each package contains.1 or more contains several packages and
classes and functions. When a modules.is Each module in turn contains several useful
project going on, as part of the project,
programmers may create packages and the related several
packages form the library. Nowadays,
top companies are sharing such
Companies can import and use thoselibrarieslibraries
with other companies freely; the other
in their own projects. Thus,
knowledge is taking place between various sharing of
companies.
280 Chapee9
ake that the goe en
follers a
ub
80601e/sub/ey/ouf/thread Yphotos
o
h lot
the root folder. In that folder, sub
goceleis
Ohaerve that nerd to import the photos py
progTAmmer,you statemens
The
hotos As a
import
your
you we qui,
00ogle.sub.py.
mport
.thread.photos follewing are the
prerarm. How
can
gU1.thread.photoe as ph
0o0g1e.sub.py. tthread. photos mport* ph
sub.py.gui.
from google.sub.py.gui.thread.phhotos import funi, fun2 Clss
Smport
google.
is the Nfer s
from

class2,
TheSpecialVariable name
When aprogramis executed in Python. there is a special variable internaly tetx
executed as name
an individual
variable storesinformation regarding
This program or as a module. When thewhether
program
the name
directly, the Python interpreter stores the value main into this variable. Whn
program isimported as a module into another program, then Python interpreter
the module name into this variable. Thus, by observing the value of the s
the programn is executed.
understand how
can
name__ we When this
written a program.
interpreter
Let'sassumestores
that
have
thewevalue main intothe special variable program
name is Hence,
run,
as a program or not as:
program is run directly
check ifthis ==main:
i f name
code is run as a program')
print('This
Suppose, if_name_variable does not contain the value main_ it representy
this code is run as a module from another external program. To understand this cone
function that displays Some message
write a program with a display()
first we will shown in Program 47.
HelloPython!". This code is

the special name variable.


Program 47: APython program using
message. save this as one.ny
#python program to display a
def display(0:
print("Hello Python!')
if name main:
display() # call display function
print(This code is run as a program')
else:
print('This code is run as a module')

You might also like