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

Similar to python_models_import_main_init_presentation.ppt (20)

PDF
Functions_in_Python.pdf text CBSE class 12
JAYASURYANSHUPEDDAPA
 
PDF
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
PPTX
Python Tutorial Part 2
Haitham El-Ghareeb
 
PDF
Using Python Libraries.pdf
SoumyadityaDey
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Python introduction
Roger Xia
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PDF
Python Imports
Vladimir Korolev
 
PPTX
package module in the python environement.pptx
MuhammadAbdullah311866
 
PPTX
Functions in Python
Kamal Acharya
 
PDF
Python. libraries. modules. and. all.pdf
prasenjitghosh1998
 
PDF
Modules and Packages in Python_Basics.pdf
RavindraTambe3
 
PDF
W-334535VBE242 Using Python Libraries.pdf
manassingh1509
 
PDF
CLTL python course: Object Oriented Programming (2/3)
Rubén Izquierdo Beviá
 
PPTX
Chapter 03 python libraries
Praveen M Jigajinni
 
PDF
ch 2. Python module
Prof .Pragati Khade
 
PPTX
Class 12 CBSE Chapter: python libraries.pptx
AravindVaithianadhan
 
PDF
Python libraries
Prof. Dr. K. Adisesha
 
PDF
Tutorial on-python-programming
Chetan Giridhar
 
PPTX
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 
Functions_in_Python.pdf text CBSE class 12
JAYASURYANSHUPEDDAPA
 
Unit-2 Introduction of Modules and Packages.pdf
Harsha Patil
 
Python Tutorial Part 2
Haitham El-Ghareeb
 
Using Python Libraries.pdf
SoumyadityaDey
 
Python Modules
Nitin Reddy Katkam
 
Python introduction
Roger Xia
 
Modules and packages in python
TMARAGATHAM
 
Python Imports
Vladimir Korolev
 
package module in the python environement.pptx
MuhammadAbdullah311866
 
Functions in Python
Kamal Acharya
 
Python. libraries. modules. and. all.pdf
prasenjitghosh1998
 
Modules and Packages in Python_Basics.pdf
RavindraTambe3
 
W-334535VBE242 Using Python Libraries.pdf
manassingh1509
 
CLTL python course: Object Oriented Programming (2/3)
Rubén Izquierdo Beviá
 
Chapter 03 python libraries
Praveen M Jigajinni
 
ch 2. Python module
Prof .Pragati Khade
 
Class 12 CBSE Chapter: python libraries.pptx
AravindVaithianadhan
 
Python libraries
Prof. Dr. K. Adisesha
 
Tutorial on-python-programming
Chetan Giridhar
 
Modules and Packages in Python Programming Language.pptx
arunavamukherjee9999
 

Recently uploaded (20)

PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PDF
Development and validation of the Japanese version of the Organizational Matt...
Yoga Tokuyoshi
 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PDF
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PDF
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
Development and validation of the Japanese version of the Organizational Matt...
Yoga Tokuyoshi
 
BinarySearchTree in datastructures in detail
kichokuttu
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
apidays Singapore 2025 - Building a Federated Future, Alex Szomora (GSMA)
apidays
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
A GraphRAG approach for Energy Efficiency Q&A
Marco Brambilla
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
1750162332_Snapshot-of-Indias-oil-Gas-data-May-2025.pdf
sandeep718278
 
Research Methodology Overview Introduction
ayeshagul29594
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
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