Powering Scientific Discovery Since 1974

# Python Tools

## Description and Overview

Python is an interpreted, general-purpose high-level programming language. Various of versions of Python are installed on most of the NERSC systems, usually accompanied with computational tool such as numpy and scipy.

## Using Python on NERSC Systems

To use the python installed as module on NERSC systems, you need do

To execute a script on the Hopper or Edison compute nodes dedicated to your job, you need to use aprun:

aprun -n 1 python ./hello_world.py

or, if the script is executable and contains '#!/usr/bin/env python' as the first line,

aprun -n 1 ./hello_world.py

## Using Numpy, Scipy, Matplotlib, ...

Several scientific computing tools for Python are installed with each Python release. You can get the version of these packages from the "Availability" table.

Numpy is a language extension that defines the numerical array and matrix type and basic operations on them.

An example numpy script:

#!/usr/bin/env python
''' Numpy example from: http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.svd.html'''

import numpy as np
a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6)
U, s, V = np.linalg.svd(a, full_matrices=True)
S = np.zeros((9, 6), dtype=complex)
S[:6, :6] = np.diag(s)
np.allclose(a, np.dot(U, np.dot(S, V)))

print "a:\n", a
print "U: \n", U
print "s: \n", s
print "V: \n", V

Scipy is another language extension that uses numpy to do advanced math, signal processing, optimization, statistics and much more.

Matplotlib is a language extension to facilitate plotting.

Mpi4py provides bindings of the MPI standard for the Python programming language. An example of using the module on a compute node is shown below:

```% cat mympi.py
#!/usr/bin/env python
from mpi4py import MPI
me = MPI.COMM_WORLD.Get_rank()
nproc = MPI.COMM_WORLD.Get_size()
print me, nproc

% cat runit
#!/bin/csh
#PBS -l mppwidth=24
#PBS -l walltime=5:00
#PBS -q debug
#PBS -j oe
cd \$PBS_O_WORKDIR
aprun -n 24 python-mpi ./mympi.py

% qsub runit
770584.edique02
% cat runit.o770584
...
9 2414 243 24
...
0 24
...```

For details about these packages, please refer to their official documentations.

## Installed Python Packages

### Individual packages in separate installation directories

Up to version 2.7.2, the python packages on Hopper and Carver and followed an all-in-one installation approach. The latest versions of selected popular packages such as numpy, scipy, ipython, matplotlib etc. were installed together with the python package under its site-packages directory. Therefore, using a particular python package meant using a certain fixed version of numpy, etc., tied to the base python package during installation. For example, using the python/2.7.1 package on Carver  means that you must use numpy/1.5.1, scipy/0.9.0b1, ipython/0.10.1, matplotlib/1.0.1, etc.

From python/2.7.3 we are taking a different approach. Individual packages are installed in their own directories, separate from the base python's site-packages directory. A module file is provided for each individual package to prepend its directory to PYTHONPATH, the search path of python for a python module. This allows multiple versions for each package, and users can mix and match by choosing the best or most suitable version for their work with any base python package.

To maintain consistency in core functionalities with pre-2.7.3 versions of python packages, a new python module (e.g., python/2.7.3) will load the following popular packages:

• python_base, which comprises of a fixed version of Python, nose, pycairo, pygobject, pygtk and setuptools
• numpy
• scipy
• matplotlib
• ipython
```% module load python% module listCurrently Loaded Modulefiles:
1) modules                   6) pgi/14.7                 11) scipy/0.14.0
2) nsg/1.2.0                 7) openmpi/1.8.1            12) matplotlib/1.3.1
3) moab/8.0.1                8) usg-default-modules/1.1  13) ipython/2.2.0
4) torque/5.0.1              9) python_base/2.7.8        14) python/2.7.8
5) OFED/2.0-3.0.0-Mellanox  10) numpy/1.8.2
```

The version numbers for the python module and its underlying python_base module (i.e., x.y.z in python/x.y.z and python_base/x.y.z, respectively) are taken from the version number of the distributed python software package (x.y.z in Python-x.y.z.tgz).  The module file for python/x.y.z also loads the default version of numpy, scipy, matplotlib and ipython. Users can swich to a different version for numpy, etc. by using a module swap command:

`% module swap numpy numpy/1.5.1`

There are many individual packages provided under this framework. The current list includes

• ase
• cython
• gpaw
• h5py
• ipython
• matplotlib
• mercurial
• mpi4py
• mysqlpython
• netcdf4-python
• numexpr
• numpy
• pil
• pycdf
• pyhdf
• pympi
• pytables
• scipy
• virtualenv

You can load these modules individually as shown below.  Note that these modules should not be used with a pre-2.7.3 version of python.

```% module load cython% module listCurrently Loaded Modulefiles:
1) modules                   6) pgi/14.7                 11) scipy/0.14.0
2) nsg/1.2.0                 7) openmpi/1.8.1            12) matplotlib/1.3.1
3) moab/8.0.1                8) usg-default-modules/1.1  13) ipython/2.2.0
4) torque/5.0.1              9) python_base/2.7.8        14) python/2.7.8
5) OFED/2.0-3.0.0-Mellanox  10) numpy/1.8.2              15) cython/0.20.2
```

### "Full-stack" versions

In addition to the above, we provide additional versions where python packages are installed all under the same base directory, following the traditional installation method. They are

• python/2.7: based on Python 2.7.x
• python/3.4: based on Python 3.4.x
• python/2.7-anaconda: 2.7 Anaconda version, based on Python 2.7.x
• python/3.4-anaconda: 3.4 Anaconda version, based on Python 3.4.x

If you use one of these, you don't have to load an individual package described in the prior section.

## Installing Your Own Python Package

Users can install their own python packages in their home directories. Here is an example installing a python package called setuptools:

• Un-tar the package and cd into the package directory

```% tar xvf setuptools-0.6c11.tar.gz% cd setuptools-0.6c11/
```
• Create a directory hierarchy for your python module libraries. Example: \$HOME/python_modules/machine_name/lib/python

• Set the PYTHONPATH environment variable

```% setenv PYTHONPATH \$HOME/python_modules/machine_name/lib/python
```
• Install your python package with the default version of python in /usr/bin/python

```% python setup.py install --home=~/python_modules/machine_name
```
• OR Install your python package with alternate version of python. (There could be a more recent version of python as a module)

```% module load python
% python setup.py install --home=~/python_modules/machine_name
```
• Test your installation

```% python
>> import setuptools
>> ...```

There is a problem with using the pip command on Hopper or Edison because of a SSL certificates verification issue. For a workaround, create certificates, ~/.pip/cabundle by running the 'mk_pip_cabundle.sh' once:

`% mk_pip_cabundle.sh`

Then, use the pip command:

`% pip --cert ~/.pip/cabundle install some_package`

Or you can use the pip command without the '--cert ~/.pip/cabundle' option if the pip configure file, ~/.pip/pip.conf, points to this file:

```% cat ~/.pip/pip.conf
[global]
cert = /global/homes/s/someone/.pip/cabundle```

## Using the DLCache library tools for large scale python applications on Hopper

Large scale Python applications may spend huge time at startup on Hopper. The DLCache library is a set of functions that can be incorporated into a dynamically-linked application to provide improved performance during the loading of dynamic libraries when running the application at large scale on Cray XE6. To access this library, do

Please read the user guide to use the tools provided by this library. This library is still in experimental stage, please report any problems, comments you see with this library to "consult at nersc dot gov".

## Availability

You can check the availability of python by using the following command:

module avail python

You can also refer to the table below for the version of installed computational tools along with python.

PackagePlatformCategoryVersionModuleInstall DateDate Made Default
Python babbage applications/ programming 2.7-anaconda python/2.7-anaconda 2015-06-04 2015-06-04
Python programming language
Python babbage applications/ programming 3.4-anaconda python/3.4-anaconda 2015-06-04
Python programming language
Python carver applications/ programming 2.6.4 python/2.6.4 2010-05-03 2010-08-25
Python programming language
Python carver applications/ programming 2.6.5 python/2.6.5 2012-01-13
Python programming language
Python carver applications/ programming 2.7-anaconda python/2.7-anaconda 2015-04-30
Python programming language
Python carver applications/ programming 2.7.1 python/2.7.1 2011-01-13 2011-08-06
Python programming language
Python carver applications/ programming 2.7.3 python/2.7.3 2012-05-03 2013-01-28
Python programming language
Python carver applications/ programming 2.7.3 python_base/2.7.3 2012-05-21 2012-05-21
Base package for Python programming language
Python carver applications/ programming 2.7.9 python/2.7.9 2015-10-08
Python programming language
Python carver applications/ programming 3.4-anaconda python/3.4-anaconda 2015-04-30
Python programming language
Python carver applications/ programming epd_7.3 python/epd_7.3 2012-11-05
Python programming language
Python carver_sl6 applications/ programming 2.7-anaconda python/2.7-anaconda 2015-04-30
Python programming language
Python carver_sl6 applications/ programming 2.7.8 python_base/2.7.8 2014-08-10 2014-08-10
Base package for Python programming language
Python carver_sl6 applications/ programming 3.4-anaconda python/3.4-anaconda 2015-04-30
Python programming language
Python cori applications/ programming 2.7-anaconda python/2.7-anaconda 2015-10-02
Python programming language
Python cori applications/ programming 2.7.10 python_base/2.7.10 2015-10-02 2015-10-02
Base package for Python programming language
Python cori applications/ programming 2.7.10 python/2.7.10 2015-10-02 2015-10-02
Python programming language
Python cori applications/ programming 3.4-anaconda python/3.4-anaconda 2015-10-02
Python programming language
Python edison applications/ programming 2.7 python/2.7 2015-04-09
Python programming language
Python edison applications/ programming 2.7-anaconda python/2.7-anaconda 2015-04-30
Python programming language
Python edison applications/ programming 2.7.3 python/2.7.3 2013-01-17 2013-01-17
Python programming language
Python edison applications/ programming 2.7.3 python_base/2.7.3 2013-01-17 2013-01-17
Base package for Python programming language
Python edison applications/ programming 2.7.5 python_base/2.7.5 2013-11-06 2013-11-19
Base package for Python programming language
Python edison applications/ programming 2.7.5 python/2.7.5 2013-11-06 2013-11-19
Python programming language
Python edison applications/ programming 2.7.9 python_base/2.7.9 2015-01-09 2015-05-20
Base package for Python programming language
Python edison applications/ programming 2.7.9 python/2.7.9 2015-05-19 2015-05-20
Python programming language
Python edison applications/ programming 3.4 python/3.4 2015-04-10
Python programming language
Python edison applications/ programming 3.4-anaconda python/3.4-anaconda 2015-04-30
Python programming language
python genepool applications/ programming 2.7.3 python/2.7.3 2012-07-05 2012-07-05
Python Programming Language
python genepool applications/ programming 2.7.3_1 python/2.7.3_1 2012-07-20 2012-07-20
Python Programming Language
python genepool applications/ programming 2.7.3_2 python/2.7.3_2 2013-01-24 2013-01-27
Python Programming Language
python genepool applications/ programming 2.7.4 python/2.7.4 2013-04-17 2013-05-21
Python Programming Language
python genepool applications/ programming 3.2.3_1 python/3.2.3_1 2012-07-28
Python Programming Language
python genepool applications/ programming 3.4.3 python/3.4.3 2015-06-25
Python Programming Language
python genepool_sl6 applications/ programming 2.7.4 python/2.7.4 2014-12-12 2014-12-12
Python Programming Language
python genepool_sl6 applications/ programming 2.7.8 python/2.7.8 2014-12-12
Python Programming Language
Python hopper_cle52 applications/ programming 2.7 python/2.7 2015-04-13
Python programming language
Python hopper_cle52 applications/ programming 2.7-anaconda python/2.7-anaconda 2015-04-30
Python programming language
Python hopper_cle52 applications/ programming 2.7.3 python_base/2.7.3 2015-03-19
Base package for Python programming language (pre-cle52 2.7.3 version - maintained for compatibility reasons)
Python hopper_cle52 applications/ programming 2.7.3 python/2.7.3 2015-03-19
Python programming language (pre-cle52 2.7.3 version - maintained for compatibility reasons)
Python hopper_cle52 applications/ programming 2.7.5 python/2.7.5 2015-04-02
Python programming language (pre-cle52 2.7.5 version - maintained for compatibility reasons)
Python hopper_cle52 applications/ programming 2.7.5 python_base/2.7.5 2015-04-02
Base package for Python programming language (pre-cle52 2.7.5 version - maintained for compatibility reasons)
Python hopper_cle52 applications/ programming 2.7.9 python_base/2.7.9 2015-03-09 2015-03-09
Base package for Python programming language
Python hopper_cle52 applications/ programming 2.7.9 python/2.7.9 2015-03-11 2015-03-11
Python programming language
Python hopper_cle52 applications/ programming 3.4 python/3.4 2015-04-13
Python programming language
Python hopper_cle52 applications/ programming 3.4-anaconda python/3.4-anaconda 2015-04-30
Python programming language
Python pdsf applications/ programming 2.6.2 python/2.6.2 2012-03-12
Python built in sl44
Python pdsf applications/ programming 2.7 python/2.7 2012-03-12
Python built in sl44
Python pdsf applications/ programming 2.7.1 python/2.7.1 2012-03-12 2012-03-12
Python built in sl44
Python pdsf_sl6 applications/ programming 2.7.10 python/2.7.10 2015-08-07
Python programming language. From anaconda installation, sanitized for NERSC
Python pdsf_sl6 applications/ programming 2.7.3 python/2.7.3 2012-10-08 2012-10-08
Python programming language. Also includes numpy, matplotlib, mysqlpython.
Python pdsf_sl6 applications/ programming 2.7.3 python_base/2.7.3 2013-07-05 2013-07-05
Python programming language. Loads only python, no extra modules.
Python pdsf_sl6 applications/ programming 2.7.6 python_base/2.7.6 2014-04-04 2014-07-07
Python programming language. Loads only python, no extra modules.
Python pdsf_sl6 applications/ programming 2.7.6 python/2.7.6 2014-04-18 2014-07-07
Python programming language. Also includes numpy, matplotlib, mysqlpython.
Python pdsf_sl6 applications/ programming 2.7.9 python/2.7.9 2015-05-11
Python programming language. From anaconda installation, sanitized for NERSC
Python pdsf_sl6 applications/ programming 3.4.3 python/3.4.3 2015-08-19
Python programming language. From anaconda installation, sanitized for NERSC
python phoebe applications/ programming 2.7.3 python/2.7.3 2013-06-18
Python Programming Language
python phoebe applications/ programming 2.7.3_1 python/2.7.3_1 2013-06-18
Python Programming Language
python phoebe applications/ programming 2.7.3_2 python/2.7.3_2 2013-06-18
Python Programming Language
python phoebe applications/ programming 2.7.4 python/2.7.4 2013-06-18 2013-06-26
Python Programming Language
python phoebe applications/ programming 3.2.3_1 python/3.2.3_1 2013-06-18
Python Programming Language
Python3 scigate applications/ programming 3.4.2 python/3.4.2 2015-01-08
Python programming language