From SciNetWiki
Jump to: navigation, 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 webpage; SciNet has given a mini-course of 8 lectures on Research Computing with Python in the Fall of 2013. An excellent set of material for teaching scientists to program in Python is also available at the Software Carpentry homepage.


Python on the GPC

We currently have several versions of python installed, compiled against fast intel math libraries. To load the python modules, type the following commands:

Version Command
2.7.2 module load gcc intel python
2.7.3 module load gcc intel/13.1.1 python/2.7.3
2.7.5 module load gcc intel/13.1.1 python/2.7.5
2.7.8 module load intel/15.0.2 python/2.7.8
2.7.11 module load anaconda2/4.0.0
2.7.13 module load anaconda2/4.3.1
3.3.4 module load gcc intel/14.0.1 python/3.3.4
3.5.1 module load anaconda3/4.0.0
3.6.1 module load anaconda3/4.4.0

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 python/2.7.2 python/2.7.3 python/2.7.5 python/2.7.8 python/3.3.4 Comments
SciPy 0.10.0 0.11.0 0.14.0 0.14.0 0.14.0 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 1.7.0 1.7.0 1.9.1 1.8.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.2 1.2.2 1.2.2 1.2.2 1.2.2 A pythonic interface to mpi. Available with openmpi; must load an openmpi module for this to work. (There is an issue with openmpi 1.4.x + infiniband, however it does appear to work fine with IntelMPI)
Numexpr 2.0 2.0.1 2.2.1 2.4 2.4_rc2 Fast, memory-efficient elementwise operations on Numpy arrays.
ScientificPython 2.8 - - - - A collection of scientific python utilities. Does not include MPI support. No longer supported.
yt 2.2 2.5.3 2.5.5 - - A collection of python tools for analyzing astrophysical simulation output.
iPython 0.11 0.13.1 1.0.0 2.3.0 1.2.1 An enhanced interactive python.
Matplotlib, pylab 1.1.0 1.2.0 1.3.0 1.4.2 1.3.1 Matlab-like plotting for python.
PyTables 2.3.1 2.4.0 3.0.0 3.1.1 3.1.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.8 1.0.4 1.1.1 - 1.1.0 Python interface to NetCDF4 files. Requires the netcdf/4.0.1_hdf5_v18-serial.shared-nofortran module to be loaded.
pyNIO 1.4.1 - - - - Yet another Python interface to NetCDF4 files; again, requires the netcdf/4.0.1_hdf5_v18-serial.shared-nofortran module. No longer supported.
h5py 2.0.1 2.1.3 2.2.0 2.3.1 2.3.0 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.
Mercurial 2.0.1 2.6.2 2.7.1 3.2 - A distributed version-control system written in Python.
Cython 0.15.1 0.18 0.19.1 0.21.1 0.20.1 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 1.2.1 1.3.0 1.3.4 1.3.0 A unit-testing framework for python.
setuptools 0.6c11 0.6c11 1.1 7.0 5.1 Enables easy installation of new python modules
pandas 0.13.0 0.13.0 0.13.0 0.15.0 0.14.1 high-performance, easy-to-use data structures and data analysis tools.
astropy - - 0.3 0.4.2 0.3.2 astronomical routines
brian 1.4.1 1.4.1 1.4.1 1.4.1 - spiking neural network simulator

Producing Matplotlib Figures on GPC Compute Nodes and in Job Scripts

The conventional way of producing figures from python using matplotlib i.e.,

   import matplotlib.pyplot as plt

will not work on the GPC compute nodes. The reason is that pyplot will try to open the figure in a window on the screen, but the compute nodes do not have screens or window managers. There is an easy workaround, however, that sets up a different 'backend' to matplotlib, one that does not try to open a window, as follows:

   import matplotlib as mpl
   import matplotlib.pyplot as plt

It is essential that the mpl.use('Agg') command precedes the importing of pyplot.

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., 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:
export PYTHONPATH=${PYTHONPATH}:${HOME}/lib/python2.X/site-packages/
  • 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:
easy_install --prefix=${HOME} -O1 [packagename]
    • Continue doing this until all of the packages you need to install are successfully installed.
    • If, upon importing the new python package, you get error messages like undefined symbol: __stack_chk_guard, you may need to use the following command instead:
LDFLAGS=-fstack-protector easy_install --prefix=${HOME} -O1 [packagename]
  • If easy_install isn't an option for your package, and the installation instructions instead talk about downloading a file and using python 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
python install --prefix=${HOME}
  • 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,
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
  • 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.

Personal tools
Knowledge Base