SlideShare a Scribd company logo
1 of 14
By James Braunsberg
What are Modules?
 Modules are files containing Python definitions and
statements (ex. name.py)
 A module’s definitions can be imported into other
modules by using “import name”
 The module’s name is available as a global variable
value
 To access a module’s functions, type “name.function()”
More on Modules
 Modules can contain executable statements along with
function definitions
 Each module has its own private symbol table used as the
global symbol table by all functions in the module
 Modules can import other modules
 Each module is imported once per interpreter session
 reload(name)
 Can import names from a module into the importing
module’s symbol table
 from mod import m1, m2 (or *)
 m1()
Executing Modules
 python name.py <arguments>
 Runs code as if it was imported
 Setting _name_ == “_main_” the file can be used as a
script and an importable module
The Module Search Path
 The interpreter searches for a file named name.py
 Current directory given by variable sys.path
 List of directories specified by PYTHONPATH
 Default path (in UNIX - .:/usr/local/lib/python)
 Script being run should not have the same name as a
standard module or an error will occur when the
module is imported
“Compiled” Python Files
 If files mod.pyc and mod.py are in the same directory,
there is a byte-compiled version of the module mod
 The modification time of the version of mod.py used to
create mod.pyc is stored in mod.pyc
 Normally, the user does not need to do anything to
create the .pyc file
 A compiled .py file is written to the .pyc
 No error for failed attempt, .pyc is recognized as invalid
 Contents of the .pyc can be shared by different
machines
Some Tips
 -O flag generates optimized code and stores it in .pyo files
 Only removes assert statements
 .pyc files are ignored and .py files are compiled to optimized bytecode
 Passing two –OO flags
 Can result in malfunctioning programs
 _doc_ strings are removed
 Same speed when read from .pyc, .pyo, or .py files, .pyo and .pyc files
are loaded faster
 Startup time of a script can be reduced by moving its code to a module
and importing the module
 Can have a .pyc or .pyo file without having a .py file for the same
module
 Module compileall creates .pyc or .pyo files for all modules in a
directory
Standard Modules
 Python comes with a library of standard modules described in
the Python Library Reference
 Some are built into interpreter
 >>> import sys
>>> sys.s1
‘>>> ‘
>>> sys.s1 = ‘c> ‘
c> print ‘Hello’
Hello
c>
 sys.path determines the interpreters’s search path for modules,
with the default path taken from PYTHONPATH
 Can be modified with append() (ex. Sys.path.append(‘SOMEPATH’)
The dir() Function
 Used to find the names a module defines and returns a
sorted list of strings
 >>> import mod
>>> dir(mod)
[‘_name_’, ‘m1’, ‘m2’]
 Without arguments, it lists the names currently
defined (variables, modules, functions, etc)
 Does not list names of built-in functions and variables
 Use _bulltin_to view all built-in functions and variables
Packages
 “dotted module names” (ex. a.b)
 Submodule b in package a
 Saves authors of multi-module packages from worrying about
each other’s module names
 Python searches through sys.path directories for the package
subdirectory
 Users of the package can import individual modules from the
package
 Ways to import submodules
 import sound.effects.echo
 from sound.effects import echo
 Submodules must be referenced by full name
 An ImportError exception is raised when the package cannot be
found
Importing * From a Package
 * does not import all submodules from a package
 Ensures that the package has been imported, only
importing the names of the submodules defined in the
package
 import sound.effects.echo
import sound.effects.surround
from sound.effects import *
Intra-package References
 Submodules can refer to each other
 Surround might use echo module
 import echo also loads surround module
 import statement first looks in the containing package
before looking in the standard module search path
 Absolute imports refer to submodules of sibling packages
 sound.filters.vocoder uses echo module
from sound.effects import echo
 Can write explicit relative imports
 from . import echo
 from .. import formats
 from ..filters import equalizer
Packages in Multiple Directories
 _path_ is a list containing the name of the directory
holding the package’s _init_.py
 Changing this variable can affect futute searches for
modules and subpackages in the package
 Can be used to extend the set of modules in a package
 Not often needed
Sources
 http://docs.python.org/tutorial/modules.html

More Related Content

Similar to jb_Modules_in_Python.ppt

Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)sroo galal
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptxAshwini Raut
 
Yii in action
Yii in actionYii in action
Yii in actionKeaNy Chu
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and ModulesOmid AmirGhiasvand
 
External dependencies ,pre init hook &amp; post init hook in odoo
External dependencies ,pre init hook &amp; post init hook in odooExternal dependencies ,pre init hook &amp; post init hook in odoo
External dependencies ,pre init hook &amp; post init hook in odooCeline George
 
Module Structure in Odoo 15
Module Structure in Odoo 15Module Structure in Odoo 15
Module Structure in Odoo 15Celine George
 
How to Build a Module in Odoo 15 Scaffold Method
How to Build a Module in Odoo 15 Scaffold MethodHow to Build a Module in Odoo 15 Scaffold Method
How to Build a Module in Odoo 15 Scaffold MethodCeline George
 
CLTL python course: Object Oriented Programming (2/3)
CLTL python course: Object Oriented Programming (2/3)CLTL python course: Object Oriented Programming (2/3)
CLTL python course: Object Oriented Programming (2/3)Rubén Izquierdo Beviá
 
package module in the python environement.pptx
package module in the python environement.pptxpackage module in the python environement.pptx
package module in the python environement.pptxMuhammadAbdullah311866
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in pythonTMARAGATHAM
 
Advanced Malware Analysis Training Session 5 - Reversing Automation
Advanced Malware Analysis Training Session 5 - Reversing AutomationAdvanced Malware Analysis Training Session 5 - Reversing Automation
Advanced Malware Analysis Training Session 5 - Reversing Automationsecurityxploded
 

Similar to jb_Modules_in_Python.ppt (20)

packages.pptx
packages.pptxpackages.pptx
packages.pptx
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Python modules
Python   modulesPython   modules
Python modules
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)
 
Python Tutorial Part 2
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
 
Yii in action
Yii in actionYii in action
Yii in action
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and Modules
 
External dependencies ,pre init hook &amp; post init hook in odoo
External dependencies ,pre init hook &amp; post init hook in odooExternal dependencies ,pre init hook &amp; post init hook in odoo
External dependencies ,pre init hook &amp; post init hook in odoo
 
Module Structure in Odoo 15
Module Structure in Odoo 15Module Structure in Odoo 15
Module Structure in Odoo 15
 
How to Build a Module in Odoo 15 Scaffold Method
How to Build a Module in Odoo 15 Scaffold MethodHow to Build a Module in Odoo 15 Scaffold Method
How to Build a Module in Odoo 15 Scaffold Method
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
 
CLTL python course: Object Oriented Programming (2/3)
CLTL python course: Object Oriented Programming (2/3)CLTL python course: Object Oriented Programming (2/3)
CLTL python course: Object Oriented Programming (2/3)
 
Python Session - 5
Python Session - 5Python Session - 5
Python Session - 5
 
Python Modules
Python ModulesPython Modules
Python Modules
 
package module in the python environement.pptx
package module in the python environement.pptxpackage module in the python environement.pptx
package module in the python environement.pptx
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Advanced Malware Analysis Training Session 5 - Reversing Automation
Advanced Malware Analysis Training Session 5 - Reversing AutomationAdvanced Malware Analysis Training Session 5 - Reversing Automation
Advanced Malware Analysis Training Session 5 - Reversing Automation
 

Recently uploaded

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 

Recently uploaded (20)

Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 

jb_Modules_in_Python.ppt

  • 2. What are Modules?  Modules are files containing Python definitions and statements (ex. name.py)  A module’s definitions can be imported into other modules by using “import name”  The module’s name is available as a global variable value  To access a module’s functions, type “name.function()”
  • 3. More on Modules  Modules can contain executable statements along with function definitions  Each module has its own private symbol table used as the global symbol table by all functions in the module  Modules can import other modules  Each module is imported once per interpreter session  reload(name)  Can import names from a module into the importing module’s symbol table  from mod import m1, m2 (or *)  m1()
  • 4. Executing Modules  python name.py <arguments>  Runs code as if it was imported  Setting _name_ == “_main_” the file can be used as a script and an importable module
  • 5. The Module Search Path  The interpreter searches for a file named name.py  Current directory given by variable sys.path  List of directories specified by PYTHONPATH  Default path (in UNIX - .:/usr/local/lib/python)  Script being run should not have the same name as a standard module or an error will occur when the module is imported
  • 6. “Compiled” Python Files  If files mod.pyc and mod.py are in the same directory, there is a byte-compiled version of the module mod  The modification time of the version of mod.py used to create mod.pyc is stored in mod.pyc  Normally, the user does not need to do anything to create the .pyc file  A compiled .py file is written to the .pyc  No error for failed attempt, .pyc is recognized as invalid  Contents of the .pyc can be shared by different machines
  • 7. Some Tips  -O flag generates optimized code and stores it in .pyo files  Only removes assert statements  .pyc files are ignored and .py files are compiled to optimized bytecode  Passing two –OO flags  Can result in malfunctioning programs  _doc_ strings are removed  Same speed when read from .pyc, .pyo, or .py files, .pyo and .pyc files are loaded faster  Startup time of a script can be reduced by moving its code to a module and importing the module  Can have a .pyc or .pyo file without having a .py file for the same module  Module compileall creates .pyc or .pyo files for all modules in a directory
  • 8. Standard Modules  Python comes with a library of standard modules described in the Python Library Reference  Some are built into interpreter  >>> import sys >>> sys.s1 ‘>>> ‘ >>> sys.s1 = ‘c> ‘ c> print ‘Hello’ Hello c>  sys.path determines the interpreters’s search path for modules, with the default path taken from PYTHONPATH  Can be modified with append() (ex. Sys.path.append(‘SOMEPATH’)
  • 9. The dir() Function  Used to find the names a module defines and returns a sorted list of strings  >>> import mod >>> dir(mod) [‘_name_’, ‘m1’, ‘m2’]  Without arguments, it lists the names currently defined (variables, modules, functions, etc)  Does not list names of built-in functions and variables  Use _bulltin_to view all built-in functions and variables
  • 10. Packages  “dotted module names” (ex. a.b)  Submodule b in package a  Saves authors of multi-module packages from worrying about each other’s module names  Python searches through sys.path directories for the package subdirectory  Users of the package can import individual modules from the package  Ways to import submodules  import sound.effects.echo  from sound.effects import echo  Submodules must be referenced by full name  An ImportError exception is raised when the package cannot be found
  • 11. Importing * From a Package  * does not import all submodules from a package  Ensures that the package has been imported, only importing the names of the submodules defined in the package  import sound.effects.echo import sound.effects.surround from sound.effects import *
  • 12. Intra-package References  Submodules can refer to each other  Surround might use echo module  import echo also loads surround module  import statement first looks in the containing package before looking in the standard module search path  Absolute imports refer to submodules of sibling packages  sound.filters.vocoder uses echo module from sound.effects import echo  Can write explicit relative imports  from . import echo  from .. import formats  from ..filters import equalizer
  • 13. Packages in Multiple Directories  _path_ is a list containing the name of the directory holding the package’s _init_.py  Changing this variable can affect futute searches for modules and subpackages in the package  Can be used to extend the set of modules in a package  Not often needed