python modules and packages
Python modules and packages:
Objective:
- The Main Module
- The Multiple Modules
- Importing a Module
- Variations of import
- Search Sequence
- Same code, Different interpretation
- Packages
- Third-party Packages
1). The Main module:
● A module is a .py file containing definitions and statements. So ,
all .py files that we created so far for our programs are modules.
● When we execute a program its module name is __main__. This
name is available in the variable __name__.
def display ()
print(" you can not make history if you use Incognito mode ")
def show ():
print(" Pizza is a pie chart of how much pizza is left ")
print(__name__)
display()
show()
On execution of this program, we get the following output
__main__
you can not make history it you use Incognito mode
Pizza is a pie chart of how much pizza is left
2). Multiple Modules:
● There are two reasons why we we may want to create a program
that contains multiple modules:
- It make sense to split a big program into multiple .py files, where each .py file acts as a module.
- Benefit - Ease of development and maintenance.
- We may two need a set of handy functions in several programs. In such a case instead of copying these functions in different program files, we may keep them in one file and use them in different programs.
3). Importing a Module:
● To use the definitions and statements in a module in another
module, we need to ' import 'it into this module.
# functions.py
def display():
print(" Earlier rich owned cars, while poor had horses: ")
def show():
print("Now everyone rich owned cars, while only rich horses:")
# use functions.py
import functions
functions.display()
functions.show()
When we execute 'usefunctions.py', it runs as a module with name __main__.
import functions makes the definitions in 'functions.py ' available in ' usefunctiions.py '.
● A module can import multiple modules.
import math
import random
import functions
a=100
b=200
print(__name__)
print( math.sin (0.5) )
print(math.cos (o.5) )
print(random.random())
print(random.random(30,40))
functions.display()
functions.show()
Here __name__ contains __main__ indicating that we are executing the main module. random and math are standard modules. functions is a user-define module.
4). Variations of import:
● The import statement can be used in multiple forms.
import math
import random
is same as
import math, random
● If we wish, we can import specific names from a module.
from math import sin cos, tan
from function import display #impot only display functions
from function import* # imports all functions
● We can rename a module while importing it. We can then use the
new name in place of the original module name.
import functions as fun
fun.display()
or even
from functions import display as disp
disp()
5). Search Sequence:
● If we import a module called ' myfuncs ', following search
sequence will be followed:
- Interpreter will first search for a built-in module called ' myfuncs '.
- If such a module is not found, then it will search for it in directory
list given by the variable sys.path.
● The list in the sys.path variable contains directory from where the
script has been execute, followed by a list of directories as
● We can print the list of directories in sys.path using:
import sys
for p in sys.path:
print(p)
6). Same Code, Diffeent Interpreter:
● Suppose we have a module called functions in ' functions,py ' If
this module has functions display() and main(). We want to use
this program sometime as an independent script, and at other as a
module form which we can use display() functions.
● To achieve this, we need to write the code in this fashion:
#functions.py
def display():
print(" Wright brothers are responsible for 9/11 too ")
def main():
print(" If you beat your own record, you win as well as lose ")
print(" Internet connects people at a long distance ")
print(" Internet disconnects people at a short distance ")
display()
if __name__ == '__main__' :
main()
If we run it as an independent program, if will satisfied. As a result, main() will be called. The name of this function need not be main().
If we import this module in another program, if will fail, so main() will not be called. However, the program can call display() independent.
7). Packages:
● The way drives, folders, subfolders help us organize files in an
OS, packages help us organize sub-packages and modules.
● A particular directory is treated as a packages if it contains a file
named __init__.py in it. The directory may contain other sub-
packages and modules in it. __init__.py file may be empty or it
may contain some initialization code for the package.
● Suppose there is a package called pkg containing a module called
mod.py. If the module contains functions f1( ) and f2 ( ) then the
directory structure would be as follows:
Directory -pkg
Contents of pkg directory -mod.py and __init__.py
Contents of mod.py -f1 ( ) and f2 ( )
● Program to use f1( ) and f2( ) would be as follows:
#mod.py
def f1( ):
print(" Inside function f1 ")
def f2( ):
print(" Inside function f2 ")
#client.py
import pkg.mod
pkg.mod.f1( )
pkg.mod.f2( )
8). Third-party Packages:
● Pythonistas in python community create software and make it
available for other programmers to use. They use PyPI --python
packages index (www.pypi.org) to distribute their software.
PyPI maintains the list of such third-party python packages
available.
● There are third-party packages available for literally doing
everything under the run.
● You too can register at PypI and upload your packages there. You
should follow the guideline given at www.pypi.org to create
package, build it and upload it to the python package Index.
● To use a package available at PyPI we need to first download it
and then install it. The installation is done using a package
manager utility called pip. pip itself is installed when python is
installed.
● Following command shows how to use pip to install a package
pykrige that has been downloaded from pypi.
Tips:
● Directory structure is very important. For a directory to quality as a
package, it has to contain a file __init__.py.
● Benefit -call to function does not need the dotted syntax.
● Limitation -Only the specified function gets imported.
● Limitation -Since there is only one function in each module,
using* is not so useful.
● Also, * is not so popular as it does not indicate which which
function / class are we importing.
python modules and packages
Reviewed by For Learnig
on
May 25, 2023
Rating:
No comments:
If you have any doubts, please tell me know