Import Modules

Modules (packages, libraries) are collections of functions that needs to be assinged (imported) with import before using it in Python.

1) function call requires module prefix

import math

x = math.log(5)

2) import all module functions to use without modul prefix

from math import *

x = log(5)

3) import only selected functions

from math import log, sqrt, sin

x = log(5)

4) change name of imported function

from math import log as ln

x = ln(5)

5) change name of imported module

import math as m

x = m.log(5)

Note, external modules needs to be locally installed on the computer before importing in Python.

Install Modules

    1. download module python-myModule-1.1.2.tar.gz

    2. unpack modul: gunzip -c python-myModule-1.1.2.tar.gz | tar xf -

    3. go to directory: cd python-myModule-1.1.2

    4. run: python setup.py install

    5. or for local installation (no root access):

    6. python setup.py install --user

    7. needs to set the path in environment variable:

    8. export PYTHONPATH=/path/to/my/library:$PYTHONPATH e.g.:

    9. export PYTHONPATH=$HOME/.local/lib/python2.6/site-packages:$PYTHONPATH

http://docs.python.org/2/install/index.html#standard-build-and-install

http://docs.python.org/2/install/index.html#alternate-installation

http://docs.python.org/3.3/install/index.html#install-index