Python

From oldwiki.scinet.utoronto.ca
Jump to navigation Jump to search

Python is programing language that continues to grow in popularity for scientific computing. It is very fast to write code in, but the software that results is much much slower than C or Fortran; one should be wary of doing too much compute-intensive work in Python. There is a dizzying amount of documentation available for programming in Python on the Python.org webpage; and an excellent set of material for teaching scientists to program in Python is available at the Software Carpentry homepage.


Python on the GPC

We currently have python 2.7.2 installed, compiled against fast intel math libraries. To use this version, <source lang=bash> module load gcc intel python </source>


Modules installed system-wide

Many optional packages are available for Python which greatly extend the language adding important new functionality. Those packages which are likely to be important to all of our users — eg, NumPy, SciPy, and Matplotlib are installed system-wide.

Below is a list of the packages currently installed system-wide.

Module Version Comments
SciPy 0.10.0 (Python 2.7.2) An Open-source software for mathematics, science, and engineering. Version in Python 2.7.x is linked against very fast MKL numerical libraries.
NumPy 1.6.1 (Python 2.7.2) NumPy is the fundamental package needed for scientific computing with Python. Contains fast arrays, tools for integrating C/C++ and Fortran code, linear algebra solvers, etc. SciPy is built on top of NumPy.
mpi4py 1.2.2 (Python 2.7.2) A pythonic interface to mpi. Available with openmpi; must load an openmpi module for this to work. (Weird note: this does *not* work with openmpi + infiniband. We're looking into it)
Numexpr 2.0 (Python 2.7.2) Fast, memory-efficient elementwise operations on Numpy arrays.
ScientificPython 2.8 A collection of scientific python utilities. Does not include MPI support.
yt 2.2 (Python 2.7.2) A collection of python tools for analyzing astrophysical simulation output.
iPython 0.11 (Python 2.7.2) An enhanced interactive python.
Matplotlib, pylab 1.1.0 (Python 2.7.2) Matlab-like plotting for python.
PyTables 2.3.1 (Python 2.7.2) Fast and efficient access to HDF5 files (and HDF5-format NetCDF4 files.) Requires the hdf5/184-p1-v18-serial-gcc module to be loaded. (hdf5/187-v18-serial-gcc on CentOS6.)
NetCDF4-python 0.9.8 (Python 2.7.2) Python interface to NetCDF4 files. Requires the netcdf/4.0.1_hdf5_v18-serial.shared-nofortran module to be loaded. (netcdf/4.1.3_hdf5_serial-gcc on CentOS 6)
pyNIO 1.4.1 (Python 2.7.2) Yet another Python interface to NetCDF4 files; again, requires the netcdf/4.0.1_hdf5_v18-serial.shared-nofortran module.
h5py 2.0.1 (Python 2.7.2) Yet another Python interface to HDF5 files; again, requires an HDF5 module to be loaded.
PySVN 1.7.1 Python interface to the svn version control system. Requires the svn module to be loaded on CentOS5.
Mercurial 2.0.1 (Python 2.7.2) A distributed version-control system written in Python.
Cython 0.15 (Python 2.7.x) Cython is a compiler which compiles Python-like code files to C code and allows them to be easily called from Python.
nose 1.1.2 (Python 2.7.2) A unit-testing framework for python.
setuptools 0.6c11 Enables easy installation of new python modules

Installing your own Python Modules

Python provides an easy way for users to install the libraries they need in their home directories rather than having them installed system-wide. There are so many optional packages for Python people could potentially want (see e.g. http://pypi.python.org/pypi), that we recommend users install these additional packages locally in their home directories. This is almost certainly the easiest way to deal with the wide range of packages, ensure they're up to date, and ensure that users' package choices don't conflict.

To install your own Python modules, follow the instructions below. Where the instructions say python2.X, type python2.6 or python2.7 depending on the version of python you are using.

  • First, create a directory in your home directory, ${HOME}/lib/python2.X/site-packages, where the packages will go.
  • Next, in your .bashrc, *after* you module load python and in the "GPC" section, add the following line:

<source lang=bash> export PYTHONPATH=${PYTHONPATH}:${HOME}/lib/python2.X/site-packages/ </source>

  • Re-load the modified .bashrc by typing source ~/.bashrc.
  • Now, if it's a standard python package and instructions say that you can use easy_intall to install it,
    • install with the following command. where packagename is the name of the package you are installing:

<source lang=bash> easy_install --prefix=${HOME} -O1 [packagename] </source>

    • Continue doing this until all of the packages you need to install are successfully installed.
  • If easy_install isn't an option for your package, and the installation instructions instead talk about downloading a file and using python setup.py install then instead:
    • Download the relevant files
    • You will probably have to uncompress and untar them: tar -xzvf packagename.tgz or tar -xjvf packagename.bz2.
    • cd into the newly created directory, and run

<source lang=bash> python setup.py install --prefix=${HOME} </source>

  • Now, the install process may have added some .egg files or directories to your path. For each .egg directory, add that to your python path as well in your .bashrc, in the same place as you had updated PYTHONPATH before: eg,

<source lang=bash> export PYTHONPATH=${PYTHONPATH}:${HOME}/lib/python2.X/site-packages:${HOME}/lib/python2.X/site-packages/packagename1-x.y.z-yy2.X.egg:${HOME}/lib/python2.X/site-packages/packagename2-a.b.c-py2.X.egg </source>

  • You should now be done! Now, re-source your .bashrc and test your new python modules.
  • In order to keep your .bashrc relatively uncluttered, and to avoid potential conflicts among software modules, we recommend that users create their own modules (for the "module" system, not specifically python modules).

Here is an example module for the Brian package, including instructions for the installation of the python Brian package itself.