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

At the time of writing, we have two versions of python installed on GPC - the default, version 2.6, and the newest in the 2.7 release series, 2.7.1, in the experimental modules. To use either, you will have to have lines like <source lang=bash> module load gcc python </source> or <source lang=bash> module load gcc use.experimental python/2.7.1 intel/intel-v12.0.0.084 </source>

in your .bashrc.

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.7.1 (Python 2.6.2); 0.9.0 (Python 2.7.1) An Open-source software for mathematics, science, and engineering. Version in Python 2.7.1 is linked against very fast MKL numerical libraries.
NumPy 1.3.0 (Python 2.6.2); 1.5.1 (Python 2.7.1) 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.1 (Python 2.6.2); 1.2.2 (Python 2.7.1) A pythonic interface to mpi. Available with openmpi; must load an openmpi module for this to work.
Numexpr 1.3.1 Fast, memory-efficient elementwise operations on Numpy arrays.
ScientificPython 2.8 A collection of scientific python utilities.
yt 1.7 (Python 2.6.2 only) A collection of python tools for analyzing astrophysical simulation output.
iPython 0.10 An enhanced interactive python.
Matplotlib, pylab 0.99.0 (Python 2.6.2); 1.0.1 (Python 2.7.1) Matlab-like plotting for python.
PyTables 2.2.1 Fast and efficient access to HDF5 files (and HDF5-format NetCDF4 files.) Requires the hdf5/184-p1-v18-serial-gcc module to be loaded.
NetCDF4-python 0.9.3 (Python 2.6.2); 0.9.4 (Python 2.7.1) Python interface to NetCDF4 files. Requires the netcdf/4.0.1_hdf5_v18-serial.shared-nofortran module to be loaded.
pyNIO 1.3.0 Another Python interface to NetCDF4 files; again, requires the netcdf/4.0.1_hdf5_v18-serial.shared-nofortran module.
PySVN 1.7.1 Python interface to the svn version control system. Requires the svn module to be loaded.
Mercurial 1.3.1 (Python 2.6.2); 1.8.2 (Python 2.7.1) A distributed version-control system written in Python.
Cython 0.12.1 (Python 2.6.2); 0.14.1 (Python 2.7.1) Cython is a compiler which compiles Python-like code files to C code and allows them to be easily called from Python.
mpi4py 1.2.1 Python interface to mpi. Requires an openmpi module to be loaded.
nose 0.11.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.