Modules

  • Reason: Your program gets bigger => need to split it to several files for easier maintenance
  • A module looks like:

  • import a module using import command


In [1]:
import module_a

print module_a.double(5)


Module A is initiating
10
  • a module is also initialize only once

In [2]:
import module_a
import module_b


Module B is initiating
  • using from statement to import name from a module directly

In [3]:
from module_b import triple

print triple(5)


15

In [4]:
from module_a import *

print 'single', single(5)
print 'double', double(5)


single 5
double 10
  • import all names will not import those beginning with underscore (_)

In [5]:
print _test_func


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-3a5f1f17a05c> in <module>()
----> 1 print _test_func

NameError: name '_test_func' is not defined

In [6]:
print module_a._test_func


<function _test_func at 0x10a119b90>

In [7]:
from module_a import _test_func

In [8]:
print _test_func


<function _test_func at 0x10a119b90>
  • global variable __name__ contains the module name.

In [9]:
print module_a.__name__


module_a

In [10]:
print __name__


__main__

Module search paths

When a module is imported

1. search for a built-in module with that name
2. search in the list of directories given by variable sys.path

sys.path is initialized from:

  • the directory where python program start, i.e: "current directory"
  • environment variable PYTHONPATH (syntax like PATH env)
  • some default locations

Packages

  • collection of module in the hierarchy structure.
  • is a folder, which have __init__.py
.
examples/
    mathlib/
        __init__.py
        linalg/
            __init__.py
            dot.py
        not_a_package/
            test.py
        ndarray.py
        random_variable.py

In [11]:
import examples


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-11-4f670c18b6bd> in <module>()
----> 1 import examples

ImportError: No module named examples

In [12]:
import mathlib


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-12-473487a6008a> in <module>()
----> 1 import mathlib

ImportError: No module named mathlib

In [1]:
import sys
sys.path.append('./examples')

In [2]:
import mathlib
  • __init__.py

In [3]:
mathlib.random_variable


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-5957f58ce91f> in <module>()
----> 1 mathlib.random_variable

AttributeError: 'module' object has no attribute 'random_variable'

In [4]:
mathlib.self_introduction()


Just a simple math library
  • import submodule

In [5]:
import mathlib.ndarray

print mathlib.ndarray.sum1d([1,2,3])


6

In [8]:
mathlib.ndarray.sum1d


Out[8]:
<function mathlib.ndarray.sum1d>

In [9]:
import mathlib.random_variable as random_variable

print random_variable.draw_uniform(0, 100)


5

In [10]:
s = random_variable

In [11]:
s.draw_uniform(0, 100)


Out[11]:
34

In [12]:
from mathlib.linalg import dot

print dot.dot1d([1,2,3], [1,2,3])


14

In [ ]: