http://www.skillbrew.com
/SkillbrewTalent brewed by the industry itself
Python modules and packages
Pavan Verma
@YinYangPavan
Founder, P3 InfoTech Solutions Pvt. Ltd.
Python Programming Essentials
© SkillBrew http://skillbrew.com
Modules
A module is a file containing Python definitions,
functions, classes and statements with extension .py
Lets say we have a python file called area.py which
has a few functions in it and we want to use those
functions again in some other programs
2
© SkillBrew http://skillbrew.com
Modules (2)
import math
def circle_area(radius):
# pi r*r
return math.pi * radius * radius
def square_area(side):
# side * side
return side * side
def rectangle_area(width, height):
# width * height
return width * height
3
area.py
© SkillBrew http://skillbrew.com
import
import area
print area.circle_area(5)
print area.square_area(20)
print area.rectangle_area(10, 20)
Output:
78.5398163397
400
200
4
math1.py
The import statement gives
access to all attributes and
methods present in module
To access methods, attributes in
module access them using
module.method syntax
© SkillBrew http://skillbrew.com
import (2)
from area import circle_area
print circle_area(5)
Output:
78.5398163397
5
math1.py
from allows you to select
specific functions from the
module
© SkillBrew http://skillbrew.com
import (3)
from area import *
print circle_area(5)
print square_area(10)
print rectangle_area(25, 10)
Output:
78.5398163397
100
250
6
math1.py
import * gives
access to all functions
in area module
You can use all the
functions in area
module directly
© SkillBrew http://skillbrew.com
Advantages of modules
 Putting code into modules is useful because of the
ability to import the module functionality
 A module allows you to logically organize your Python
code
 Grouping related code into a module makes the code
easier to understand and use
7
© SkillBrew http://skillbrew.com
Packages
 A Python package is simply a directory of Python module(s)
 Lets say we have another module called volumes.py
which has a few functions to compute volumes, now it
would make sense that area.py and volume.py are
both related and should be bundled in a package
 We are going to create a package geometry which will have
both modules (area.py and volume.py)
8
© SkillBrew http://skillbrew.com
Packages (2)
geometry
area volume
9
• Create a directory named geometry and add both area.py and
volume.py in it
• Also create a file __init__.py in directory geometry
• The __init__.py files are required to make Python treat the
directories as containing packages
© SkillBrew http://skillbrew.com
Packages (3)
10
from geometry import area, volume
print area.rectangle_area(25, 10)
print volume.cube_volume(10)
Output:
250
1000
math1.py
© SkillBrew http://skillbrew.com
Advantages of packages
 Packages are a way of structuring Python’s module
namespace by using “dotted module names”. For
example, the module name A.B designates a
sub module named B in a package named A
 The use of dotted module names saves the authors of
multi-module packages like NumPy or the Python
Imaging Library from having to worry about each
other’s module names
11
© SkillBrew http://skillbrew.com
Locating modules
 When you import a module, the Python interpreter searches
for the module in the following sequences:
• The current directory
• If the module isn't found, Python then searches each
directory in the shell variable PYTHONPATH
• If all else fails, Python checks the default path. On UNIX, this
default path is normally
/usr/local/lib/python/
12
© SkillBrew http://skillbrew.com
main block
 Before python runs any module it sets up a bunch of
special variables of which one of them is __name__
 __name__ is set equal to "__main__" when the
module is run as standalone program
 If a module is imported the __name__ attribute for
that module is equal to its name
13
© SkillBrew http://skillbrew.com
main block (2)
14
import math
def circle_area(radius):
return math.pi * radius * radius
def square_area(side):
return side * side
def rectangle_area(width, height):
return width * height
if __name__ == '__main__':
print square_area(5)
area.py
We have updated area.py
in geometry package a bit,
if __name__ == '__main__'
block has been added
© SkillBrew http://skillbrew.com
main block (3)
 The reason to add a block like
if __name__ == '__main__': to the module is that we want to
be able to test/run the module separately other than just importing it and
using it
 This is where __name__ comes into play when we run the module area.py
as a stand alone program like this
$ python area.py
the __name__ attribute becomes equal to __main__ hence the block
if __name__ == '__main__': is executed
 Now if you import this module in other programs, in that case the value of
attribute __name__ for area.py would be geometry.area hence the main
block in area.py does not get executed
15
© SkillBrew http://skillbrew.com
Summary
 Modules
 import
 Advantages of imports
 Packages
 Advantages of packages
 main block
16
© SkillBrew http://skillbrew.com
Resources
 Tutorial on modules http://net.tutsplus.com/tutorials/python-
tutorials/python-from-scratch-functions-and-modules-2/
 Packages Python docs
http://docs.python.org/2/tutorial/modules.html#packages
 __name__ explanation on stackoverflow
http://stackoverflow.com/questions/419163/what-does-if-name-main-do
 module’s __name__ attribute
http://ibiblio.org/g2swap/byteofpython/read/module-name.html
17
18

Python Programming Essentials - M18 - Modules and Packages

  • 1.
    http://www.skillbrew.com /SkillbrewTalent brewed bythe industry itself Python modules and packages Pavan Verma @YinYangPavan Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
  • 2.
    © SkillBrew http://skillbrew.com Modules Amodule is a file containing Python definitions, functions, classes and statements with extension .py Lets say we have a python file called area.py which has a few functions in it and we want to use those functions again in some other programs 2
  • 3.
    © SkillBrew http://skillbrew.com Modules(2) import math def circle_area(radius): # pi r*r return math.pi * radius * radius def square_area(side): # side * side return side * side def rectangle_area(width, height): # width * height return width * height 3 area.py
  • 4.
    © SkillBrew http://skillbrew.com import importarea print area.circle_area(5) print area.square_area(20) print area.rectangle_area(10, 20) Output: 78.5398163397 400 200 4 math1.py The import statement gives access to all attributes and methods present in module To access methods, attributes in module access them using module.method syntax
  • 5.
    © SkillBrew http://skillbrew.com import(2) from area import circle_area print circle_area(5) Output: 78.5398163397 5 math1.py from allows you to select specific functions from the module
  • 6.
    © SkillBrew http://skillbrew.com import(3) from area import * print circle_area(5) print square_area(10) print rectangle_area(25, 10) Output: 78.5398163397 100 250 6 math1.py import * gives access to all functions in area module You can use all the functions in area module directly
  • 7.
    © SkillBrew http://skillbrew.com Advantagesof modules  Putting code into modules is useful because of the ability to import the module functionality  A module allows you to logically organize your Python code  Grouping related code into a module makes the code easier to understand and use 7
  • 8.
    © SkillBrew http://skillbrew.com Packages A Python package is simply a directory of Python module(s)  Lets say we have another module called volumes.py which has a few functions to compute volumes, now it would make sense that area.py and volume.py are both related and should be bundled in a package  We are going to create a package geometry which will have both modules (area.py and volume.py) 8
  • 9.
    © SkillBrew http://skillbrew.com Packages(2) geometry area volume 9 • Create a directory named geometry and add both area.py and volume.py in it • Also create a file __init__.py in directory geometry • The __init__.py files are required to make Python treat the directories as containing packages
  • 10.
    © SkillBrew http://skillbrew.com Packages(3) 10 from geometry import area, volume print area.rectangle_area(25, 10) print volume.cube_volume(10) Output: 250 1000 math1.py
  • 11.
    © SkillBrew http://skillbrew.com Advantagesof packages  Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a sub module named B in a package named A  The use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names 11
  • 12.
    © SkillBrew http://skillbrew.com Locatingmodules  When you import a module, the Python interpreter searches for the module in the following sequences: • The current directory • If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH • If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/ 12
  • 13.
    © SkillBrew http://skillbrew.com mainblock  Before python runs any module it sets up a bunch of special variables of which one of them is __name__  __name__ is set equal to "__main__" when the module is run as standalone program  If a module is imported the __name__ attribute for that module is equal to its name 13
  • 14.
    © SkillBrew http://skillbrew.com mainblock (2) 14 import math def circle_area(radius): return math.pi * radius * radius def square_area(side): return side * side def rectangle_area(width, height): return width * height if __name__ == '__main__': print square_area(5) area.py We have updated area.py in geometry package a bit, if __name__ == '__main__' block has been added
  • 15.
    © SkillBrew http://skillbrew.com mainblock (3)  The reason to add a block like if __name__ == '__main__': to the module is that we want to be able to test/run the module separately other than just importing it and using it  This is where __name__ comes into play when we run the module area.py as a stand alone program like this $ python area.py the __name__ attribute becomes equal to __main__ hence the block if __name__ == '__main__': is executed  Now if you import this module in other programs, in that case the value of attribute __name__ for area.py would be geometry.area hence the main block in area.py does not get executed 15
  • 16.
    © SkillBrew http://skillbrew.com Summary Modules  import  Advantages of imports  Packages  Advantages of packages  main block 16
  • 17.
    © SkillBrew http://skillbrew.com Resources Tutorial on modules http://net.tutsplus.com/tutorials/python- tutorials/python-from-scratch-functions-and-modules-2/  Packages Python docs http://docs.python.org/2/tutorial/modules.html#packages  __name__ explanation on stackoverflow http://stackoverflow.com/questions/419163/what-does-if-name-main-do  module’s __name__ attribute http://ibiblio.org/g2swap/byteofpython/read/module-name.html 17
  • 18.