SlideShare a Scribd company logo
1 of 15
By Shanmugapriya Shanmugam
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()
 A Python module is a file that consists of Python code.
 It allows us to logically arrange related code and makes
the code easier to understand and use. It defines
functions, classes and variables.
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
Example
 For example, math is a built-in module that offers
several built-in functions.
 import math
 print dir(math)
 Output:
 [‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘acos’,
‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’,
‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’, ‘erfc’, ‘exp’,
‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’,
‘gamma’, ‘hypot’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’,
‘log10’, ‘log1p’, ‘modf’, ‘pi’, ‘pow’, ‘radians’, ‘sin’, ‘sinh’,
‘sqrt’, ‘tan’, ‘tanh’, ‘trunc’]
Sources
 http://docs.python.org/tutorial/modules.html

More Related Content

What's hot (20)

Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python Generators
Python GeneratorsPython Generators
Python Generators
 
Python Programming ppt
Python Programming pptPython Programming ppt
Python Programming ppt
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
user defined function
user defined functionuser defined function
user defined function
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Modules
Python ModulesPython Modules
Python Modules
 
Python functions
Python functionsPython functions
Python functions
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Python basics
Python basicsPython basics
Python basics
 

Similar to Python modules

jb_Modules_in_Python.ppt
jb_Modules_in_Python.pptjb_Modules_in_Python.ppt
jb_Modules_in_Python.pptloliktry
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxSingamvineela
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptxkrushnaraj1
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdfSoumyadityaDey
 
Python import mechanism
Python import mechanismPython import mechanism
Python import mechanismYuki Nishiwaki
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programmingsambitmandal
 
Module Structure in Odoo 15
Module Structure in Odoo 15Module Structure in Odoo 15
Module Structure in Odoo 15Celine George
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptxAshwini Raut
 
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
 
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
 
Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)sroo galal
 
Composition of a Module in Odoo 16
Composition of a Module in Odoo 16Composition of a Module in Odoo 16
Composition of a Module in Odoo 16Celine George
 

Similar to Python modules (20)

jb_Modules_in_Python.ppt
jb_Modules_in_Python.pptjb_Modules_in_Python.ppt
jb_Modules_in_Python.ppt
 
Python modules
Python modulesPython modules
Python modules
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptx
 
Functions_in_Python.pptx
Functions_in_Python.pptxFunctions_in_Python.pptx
Functions_in_Python.pptx
 
Using Python Libraries.pdf
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
 
packages.pptx
packages.pptxpackages.pptx
packages.pptx
 
Python import mechanism
Python import mechanismPython import mechanism
Python import mechanism
 
Python modules
Python   modulesPython   modules
Python modules
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Module Structure in Odoo 15
Module Structure in Odoo 15Module Structure in Odoo 15
Module Structure in Odoo 15
 
Functions and Modules.pptx
Functions and Modules.pptxFunctions and Modules.pptx
Functions and Modules.pptx
 
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
 
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
 
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
 
Composition of a Module in Odoo 16
Composition of a Module in Odoo 16Composition of a Module in Odoo 16
Composition of a Module in Odoo 16
 
Python Session - 5
Python Session - 5Python Session - 5
Python Session - 5
 

Recently uploaded

Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 

Recently uploaded (20)

Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

Python modules

  • 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()  A Python module is a file that consists of Python code.  It allows us to logically arrange related code and makes the code easier to understand and use. It defines functions, classes and variables.
  • 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
  • 14. Example  For example, math is a built-in module that offers several built-in functions.  import math  print dir(math)  Output:  [‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘hypot’, ‘isinf’, ‘isnan’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘modf’, ‘pi’, ‘pow’, ‘radians’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘trunc’]