Python
Modules
What are Python modules?
 Files
 with previously written code that can be
 imported to the Python interpreter.
A module contains its own public and private
 variables to prevent interferences with user
 defined ones.
 Modules file have .py extension
An example of a module
$ vim modex.py
 def cube(x):
 //Returns the value of x^3
 return x*x*x,
Importing Modules
 >>> import modex
 >>> modex.cube(6)
 216
 >>> y = modex.cube(6)
 >>>y
 216
Accessing global variables in a
module
 Using dot notation.
 >>> a = anymodule.globalvar
 Notethat a global variables will not affect another
 module; i.e. modify module1.gvar will not change
 the value of module2.gvar.
From/import statements
 Used to import specific functions from a module
 >>> from modex import cube
 >>> cube(3)
 >>>27
Executing modules as scripts
 Python modules can be executed in command line
 whith
 python modex.py <arguments>
Compiled Python files
 When importing modex.py, a modex.pyc file is
 generated.
 It contains the bytecode from the last time
 modex.py is imported.
 If there is no .pyc file exists, it is created
 For future imports, these .pyc files are used to
 speed up the loading time, not the execution time
The dir() function
 This function returns a list of modules and
 functions that have been imported
 >>> dir()
 [‘_ _name_ _’, ‘_ _package_ _’]
 >>>import modex
 >>>dir()
 [‘_ _name_ _’, ‘_ _package_ _’,’_ _modex’]
 Standard Modules
 Python comes with a library of standard modules.
 These modules contains built-in operations that are not
 actually part of Python’s core
 Some of them are only imported based on the OS. For
 instance: winreg module is only provided on Windows
 systems.
 An important Std module is sys. it is used to change the search
 path for the module
 >>> sys.path.append(‘/home/pnp29/cs265_spring_2011
Standard modules
 There is a standard library of Python modules
 These modules contain built-in operations that are
 not actually a part of Python at its core
 Some modules are only imported if a certain
 operating system is being used
 One important library module is sys
The sys module
 Two variable contained in sys, ps1 and ps2, hold the
 values of the strings in the primary and secondary
 prompts. These strings can be changed to your
 preference.
>>> import sys
>>> sys.ps1 = 'plurt> '
plurt> print ‘plurt'
plurt
plurt>
The dir() function
 The dir() function returns a list of names (variables
 and functions) that are defined by the module
 passed to it.
 >>> dir(sqrt)
 [‘num', ‘result']
 When dir is executed with no arguments, it returns
 a list of all currently defined names, except those
 that are built-in
Packages
 Packages are organized collections of modules,
 modules within modules
 Modules are stored in directories, with each
 directory containing a __init__.py file that tells the
 interpreter that it is a package directory
 Each subdirectory of root package directory is
 considered a subpackage
Packages, cont’d
 The package or any of its subpackages may then
 be imported.
 import currency.conversion.euro
 Any of euro’s definitions or subpackages can now
 be accessed, but only using dots:
 euro.convertfromdollar(10)
Importing * from a package
 Sometimes you may want to import everything
 inside a package or subpackage. In this case you
 may use this command:
 from example.subexample import *
 This loads all module names contained in variable
 __all__, which is itself contained in the package
 directory’s __init__.py file.
Importing * from a package,
cont’d
 If__all__ variable is not defined, then by default,
 importing * from a package will import all defined
 names of the package, and the package itself. Any
 submodules will not be imported unless they are
 explicitly imported by __init__.py or by
Intra-package references
 Any module in a package may import any other
 modules in the same directory by name without
 using any dots
 For importing any modules in a sibling folder, a
 module may use an absolute import
Packages in multiple directories
 The location in which a package searches for its
 __init__.py may be changed, by modifying the
 variable __path__, which stores the value of the
 directory the __init__.py is in
Packages
 They are organized collections of modules.
 Modules are stored in directories
 Each directory contains a __init__.py file which
 tells the Python interpreter that it is a package
 directory.
Example of a package
 math/
  __init__.py
  triArea.py
  sqArea.py
  statistics/
  __init__.py
  meanVal.py
Example of using packages
 >>> import math.sqArea
 >>> math.sqArea.area(5)
 25
 >>> from math.sqArea import area
 >>> area(5)
 25
Questions?
Sources
 http://docs.python.org/tutorial/modules.html