Python Scientific lecture notes, Release 2012.2
The list of directories searched by Python is given by the sys.path variable
In [1]: import sys
In [2]: sys.path
Out[2]:
[’’,
’/usr/bin’,
’/usr/local/include/enthought.traits-1.1.0’,
’/usr/lib/python2.6’,
’/usr/lib/python2.6/plat-linux2’,
’/usr/lib/python2.6/lib-tk’,
’/usr/lib/python2.6/lib-old’,
’/usr/lib/python2.6/lib-dynload’,
’/usr/lib/python2.6/dist-packages’,
’/usr/lib/pymodules/python2.6’,
’/usr/lib/pymodules/python2.6/gtk-2.0’,
’/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode’,
’/usr/local/lib/python2.6/dist-packages’,
’/usr/lib/python2.6/dist-packages’,
’/usr/lib/pymodules/python2.6/IPython/Extensions’,
u’/home/gouillar/.ipython’]
Modules must be located in the search path, therefore you can:
• write your own modules within directories already defined in the search path (e.g.
‘/usr/local/lib/python2.6/dist-packages’). You may use symbolic links (on Linux) to keep the code
somewhere else.
• modify the environment variable PYTHONPATH to include the directories containing the user-defined
modules. On Linux/Unix, add the following line to a file read by the shell at startup (e.g. /etc/profile,
.profile)
export PYTHONPATH=$PYTHONPATH:/home/emma/user_defined_modules
On Windows, http://support.microsoft.com/kb/310519 explains how to handle environment variables.
• or modify the sys.path variable itself within a Python script.
import sys
new_path = ’/home/emma/user_defined_modules’
if new_path not in sys.path:
sys.path.append(new_path)
This method is not very robust, however, because it makes the code less portable (user-dependent path) and because
you have to add the directory to your sys.path each time you want to import from a module in this directory.
See http://docs.python.org/tutorial/modules.html for more information about modules.
2.6.6 Packages
A directory that contains many modules is called a package. A package is a module with submodules (which can
have submodules themselves, etc.). A special file called __init__.py (which may be empty) tells Python that
the directory is a Python package, from which modules can be imported.
sd-2116 /usr/lib/python2.6/dist-packages/scipy $ ls
[17:07]
cluster/ io/ README.txt@ stsci/
__config__.py@ LATEST.txt@ setup.py@ __svn_version__.py@
__config__.pyc lib/ setup.pyc __svn_version__.pyc
constants/ linalg/ setupscons.py@ THANKS.txt@
fftpack/ linsolve/ setupscons.pyc TOCHANGE.txt@
__init__.py@ maxentropy/ signal/ version.py@
2.6. Reusing code: scripts and modules 29
Python Scientific lecture notes, Release 2012.2
__init__.pyc misc/ sparse/ version.pyc
INSTALL.txt@ ndimage/ spatial/ weave/
integrate/ odr/ special/
interpolate/ optimize/ stats/
sd-2116 /usr/lib/python2.6/dist-packages/scipy $ cd ndimage
[17:07]
sd-2116 /usr/lib/python2.6/dist-packages/scipy/ndimage $ ls
[17:07]
doccer.py@ fourier.pyc interpolation.py@ morphology.pyc setup.pyc
doccer.pyc info.py@ interpolation.pyc _nd_image.so
setupscons.py@
filters.py@ info.pyc measurements.py@ _ni_support.py@
setupscons.pyc
filters.pyc __init__.py@ measurements.pyc _ni_support.pyc tests/
fourier.py@ __init__.pyc morphology.py@ setup.py@
From Ipython:
In [1]: import scipy
In [2]: scipy.__file__
Out[2]: ’/usr/lib/python2.6/dist-packages/scipy/__init__.pyc’
In [3]: import scipy.version
In [4]: scipy.version.version
Out[4]: ’0.7.0’
In [5]: import scipy.ndimage.morphology
In [6]: from scipy.ndimage import morphology
In [17]: morphology.binary_dilation ?
Type: function
Base Class: <type ’function’>
String Form: <function binary_dilation at 0x9bedd84>
Namespace: Interactive
File: /usr/lib/python2.6/dist-packages/scipy/ndimage/morphology.py
Definition: morphology.binary_dilation(input, structure=None,
iterations=1, mask=None, output=None, border_value=0, origin=0,
brute_force=False)
Docstring:
Multi-dimensional binary dilation with the given structure.
An output array can optionally be provided. The origin parameter
controls the placement of the filter. If no structuring element is
provided an element is generated with a squared connectivity equal
to one. The dilation operation is repeated iterations times. If
iterations is less than 1, the dilation is repeated until the
result does not change anymore. If a mask is given, only those
elements with a true value at the corresponding mask element are
modified at each iteration.
2.6.7 Good practices
Note: Good practices
• Indentation: no choice!
Indenting is compulsory in Python. Every commands block following a colon bears an additional indentation level
2.6. Reusing code: scripts and modules 30