SlideShare a Scribd company logo
 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()”
 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()
 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 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
 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
 -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
 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’)
 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
 “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
 * 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 *
 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
 _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
 http://docs.python.org/tutorial/modules.html

More Related Content

PPT
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
PPT
jb_Modules_in_Python.ppt
PPT
Python modules
PPTX
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
PPTX
Python Modules, executing modules as script.pptx
PPTX
Python module 3, b.tech 5th semester ppt
PPTX
Functions_in_Python.pptx
mod.ppt mod.ppt mod.ppt mod.ppt mod.pp d
jb_Modules_in_Python.ppt
Python modules
jb_Modules_in_Python.pptx jb_Modules_in_Python.pptx
Python Modules, executing modules as script.pptx
Python module 3, b.tech 5th semester ppt
Functions_in_Python.pptx

Similar to python_models_import_main_init_presentation.ppt (20)

PPTX
CLASS-11 & 12 ICT PPT Functions in Python.pptx
PDF
Functions_in_Python.pdf text CBSE class 12
PPTX
Class 12 CBSE Chapter: python libraries.pptx
PDF
Using Python Libraries.pdf
PDF
Python import mechanism
PPTX
packages.pptx
PPTX
Modules in Python Programming
PPTX
library-basics python.pptx for education
PDF
Unit-2 Introduction of Modules and Packages.pdf
PDF
Modules and Packages in Python_Basics.pdf
ODP
Pythonpresent
PDF
Python modules
PPTX
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
PPTX
Chapter 03 python libraries
PDF
Python libraries
PPTX
Using python libraries.pptx , easy ppt to study class 12
PDF
PYTHON Programming compilation COMMAND LINE TOOLS.pdf
PPTX
Python for Beginners
PDF
Python. libraries. modules. and. all.pdf
PPTX
Functions and Modules.pptx
CLASS-11 & 12 ICT PPT Functions in Python.pptx
Functions_in_Python.pdf text CBSE class 12
Class 12 CBSE Chapter: python libraries.pptx
Using Python Libraries.pdf
Python import mechanism
packages.pptx
Modules in Python Programming
library-basics python.pptx for education
Unit-2 Introduction of Modules and Packages.pdf
Modules and Packages in Python_Basics.pdf
Pythonpresent
Python modules
7-_Modules__Packagesyyyyyyyyyyyyyyyyyyyyyyyyyyyyy.pptx
Chapter 03 python libraries
Python libraries
Using python libraries.pptx , easy ppt to study class 12
PYTHON Programming compilation COMMAND LINE TOOLS.pdf
Python for Beginners
Python. libraries. modules. and. all.pdf
Functions and Modules.pptx
Ad

Recently uploaded (20)

PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PDF
Foundation of Data Science unit number two notes
PDF
Mega Projects Data Mega Projects Data
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PPTX
Database Infoormation System (DBIS).pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
Launch Your Data Science Career in Kochi – 2025
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Computer network topology notes for revision
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
Introduction to Business Data Analytics.
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
Fluorescence-microscope_Botany_detailed content
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Introduction-to-Cloud-ComputingFinal.pptx
Foundation of Data Science unit number two notes
Mega Projects Data Mega Projects Data
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Moving the Public Sector (Government) to a Digital Adoption
Database Infoormation System (DBIS).pptx
Miokarditis (Inflamasi pada Otot Jantung)
Launch Your Data Science Career in Kochi – 2025
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Computer network topology notes for revision
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Introduction to Business Data Analytics.
1_Introduction to advance data techniques.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Fluorescence-microscope_Botany_detailed content
Ad

python_models_import_main_init_presentation.ppt

  • 1.  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()”
  • 2.  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()
  • 3.  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
  • 4.  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
  • 5.  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
  • 6.  -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
  • 7.  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’)
  • 8.  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
  • 9.  “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
  • 10.  * 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 *
  • 11.  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
  • 12.  _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