SlideShare a Scribd company logo
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

More Related Content

PDF
What Are Python Modules? Edureka
PPTX
Python Programming Essentials - M17 - Functions
ODP
Python Modules
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
Python Programming Essentials - M34 - List Comprehensions
PPTX
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
PPT
Profiling in python
PPTX
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
What Are Python Modules? Edureka
Python Programming Essentials - M17 - Functions
Python Modules
Python Programming Essentials - M25 - os and sys modules
Python Programming Essentials - M34 - List Comprehensions
Python Programming Essentials - M37 - Brief Overview of Misc Concepts
Profiling in python
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...

What's hot (20)

PPTX
Python Programming Essentials - M9 - String Formatting
PPTX
Packages and Datastructures - Python
PPSX
Modules and packages in python
PPTX
Python Programming Essentials - M31 - PEP 8
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
Python Programming Essentials - M7 - Strings
PPTX
Python Programming Essentials - M6 - Code Blocks and Indentation
PPTX
Python Programming Essentials - M28 - Debugging with pdb
PPTX
PYTHON PROGRAMMING
PPTX
Python Programming Essentials - M13 - Tuples
PPT
Python advanced 3.the python std lib by example – application building blocks
PPT
Lecture#6 functions in c++
PPT
Functions in c++
ODP
C++ Function
PPTX
C language header files
PDF
Python lambda functions with filter, map & reduce function
PPTX
Python Programming Essentials - M5 - Variables
DOCX
UNIT 4-HEADER FILES IN C
PPTX
Inline Functions and Default arguments
PPTX
Inline function
Python Programming Essentials - M9 - String Formatting
Packages and Datastructures - Python
Modules and packages in python
Python Programming Essentials - M31 - PEP 8
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Programming Essentials - M7 - Strings
Python Programming Essentials - M6 - Code Blocks and Indentation
Python Programming Essentials - M28 - Debugging with pdb
PYTHON PROGRAMMING
Python Programming Essentials - M13 - Tuples
Python advanced 3.the python std lib by example – application building blocks
Lecture#6 functions in c++
Functions in c++
C++ Function
C language header files
Python lambda functions with filter, map & reduce function
Python Programming Essentials - M5 - Variables
UNIT 4-HEADER FILES IN C
Inline Functions and Default arguments
Inline function
Ad

Viewers also liked (14)

PPTX
Python Programming Essentials - M3 - Python Installation
PPTX
Python Programming Essentials - M22 - File Operations
PPTX
Python Programming Essentials - M1 - Course Introduction
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPTX
Python Programming Essentials - M2 - Introduction to Python
PDF
Python Programming - X. Exception Handling and Assertions
PDF
Python exceptions
PDF
Sara Afshar: Scheduling and Resource Sharing in Multiprocessor Real-Time Systems
PPTX
The Awesome Python Class Part-4
PDF
Python Programming - XII. File Processing
PDF
Using Python Packages - An Overview
ODP
Python Presentation
PPT
Introduction to Python
PPT
Free Download Powerpoint Slides
Python Programming Essentials - M3 - Python Installation
Python Programming Essentials - M22 - File Operations
Python Programming Essentials - M1 - Course Introduction
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M2 - Introduction to Python
Python Programming - X. Exception Handling and Assertions
Python exceptions
Sara Afshar: Scheduling and Resource Sharing in Multiprocessor Real-Time Systems
The Awesome Python Class Part-4
Python Programming - XII. File Processing
Using Python Packages - An Overview
Python Presentation
Introduction to Python
Free Download Powerpoint Slides
Ad

Similar to Python Programming Essentials - M18 - Modules and Packages (20)

PDF
Using Python Libraries.pdf
PDF
Modules and Packages in Python_Basics.pdf
PDF
class 12 Libraries in dfbdsbsdbdfbdfdfdf.pdf
PDF
Python. libraries. modules. and. all.pdf
PPTX
package module in the python environement.pptx
PPTX
Python for Beginners
PPTX
Python module 3, b.tech 5th semester ppt
PPTX
Python programming workshop session 4
DOCX
Modules in Python.docx
PDF
Unit-2 Introduction of Modules and Packages.pdf
PPTX
Object oriented programming design and implementation
PDF
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
PDF
ch 2. Python module
PDF
Modules 101
PDF
Python.pdf
PPTX
Class 12 CBSE Chapter: python libraries.pptx
PDF
Python basics
PDF
Python basics
PDF
Python libraries
Using Python Libraries.pdf
Modules and Packages in Python_Basics.pdf
class 12 Libraries in dfbdsbsdbdfbdfdfdf.pdf
Python. libraries. modules. and. all.pdf
package module in the python environement.pptx
Python for Beginners
Python module 3, b.tech 5th semester ppt
Python programming workshop session 4
Modules in Python.docx
Unit-2 Introduction of Modules and Packages.pdf
Object oriented programming design and implementation
Python Programming - IV. Program Components (Functions, Classes, Modules, Pac...
ch 2. Python module
Modules 101
Python.pdf
Class 12 CBSE Chapter: python libraries.pptx
Python basics
Python basics
Python libraries

More from P3 InfoTech Solutions Pvt. Ltd. (16)

PPTX
Python Programming Essentials - M44 - Overview of Web Development
PPTX
Python Programming Essentials - M40 - Invoking External Programs
PPTX
Python Programming Essentials - M39 - Unit Testing
PPTX
Python Programming Essentials - M35 - Iterators & Generators
PPTX
Python Programming Essentials - M29 - Python Interpreter and Files
PPTX
Python Programming Essentials - M27 - Logging module
PPTX
Python Programming Essentials - M24 - math module
PPTX
Python Programming Essentials - M23 - datetime module
PPTX
Python Programming Essentials - M21 - Exception Handling
PPTX
Python Programming Essentials - M20 - Classes and Objects
PPTX
Python Programming Essentials - M15 - References
PPTX
Python Programming Essentials - M14 - Dictionaries
PPTX
Python Programming Essentials - M12 - Lists
PPTX
Python Programming Essentials - M11 - Comparison and Logical Operators
PPTX
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
PPTX
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M44 - Overview of Web Development
Python Programming Essentials - M40 - Invoking External Programs
Python Programming Essentials - M39 - Unit Testing
Python Programming Essentials - M35 - Iterators & Generators
Python Programming Essentials - M29 - Python Interpreter and Files
Python Programming Essentials - M27 - Logging module
Python Programming Essentials - M24 - math module
Python Programming Essentials - M23 - datetime module
Python Programming Essentials - M21 - Exception Handling
Python Programming Essentials - M20 - Classes and Objects
Python Programming Essentials - M15 - References
Python Programming Essentials - M14 - Dictionaries
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M11 - Comparison and Logical Operators
Python Programming Essentials - M10 - Numbers and Artihmetic Operators
Python Programming Essentials - M8 - String Methods

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
sap open course for s4hana steps from ECC to s4
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Machine learning based COVID-19 study performance prediction
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Spectroscopy.pptx food analysis technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Electronic commerce courselecture one. Pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
Digital-Transformation-Roadmap-for-Companies.pptx
Understanding_Digital_Forensics_Presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
sap open course for s4hana steps from ECC to s4
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Machine learning based COVID-19 study performance prediction
Advanced methodologies resolving dimensionality complications for autism neur...
Spectroscopy.pptx food analysis technology
NewMind AI Weekly Chronicles - August'25 Week I
Electronic commerce courselecture one. Pdf
Programs and apps: productivity, graphics, security and other tools
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Python Programming Essentials - M18 - Modules and Packages

  • 1. 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
  • 2. © 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
  • 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 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
  • 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 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
  • 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 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
  • 12. © 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
  • 13. © 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
  • 14. © 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
  • 15. © 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
  • 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. 18