Description

Python is a general purpose programming and scripting language. There are a large number of additional modules/libraries available for a wide range of purposes, including many for numerical and scientific computing.

Availability

Python is installed for all Flux services and typically several versions are available. We also install a few additional libraries that have proven to be of general use and widely used. See Python standard libraries for the list.

In addition to the version of Python that is compiled specifically for Flux, we also have available a precompiled version that bundles a large number of libraries, which can be very convenient. This is the Enthought Python Distribution (EPD). For more details, see Python distributions. You should only use one of EPD or one of our Python modules on Flux, not both, as they both provide the python command.

The operating system also comes with a version of Python. Sometimes it is appropriate to use that, but we generally recommend that you use one of our Python modules or one of the EPD modules.

Accessing python

To see which versions of Python are currently available on Flux, use

$ module available python

To see which versions of the EPD are available, use

$ module available epd

To use the python or epd modules, use

$ module load python/2.7.5

or

$ module load epd

Running Python interactively

Python can be run in “true” interactive mode simply with

$ python
>>>

The prompt changes to Python’s >>> prompt. To exit, you can use either the Python quit() function, or Ctrl-D at the triple-chevron prompt.

Running Python in batch mode

More often, you will have a Python script to run, in which case you provide the filename of the script as an argument. Python scripts often have the .py extension, but it is not required. For example, if you have this line in a file called hello.py

print "Hello, world."

then running it would look like the following transcript.

$ python hello.py
Hello, world.

Most Python use will be with programs or scripts, typically stored in files with a .py extension. When Python runs a script, it will create a compiled version, which will have a .pyc extension; do not try to run those directly.

You can also create a Python script that can be run as a command. For example, create a file called python-example that contains the lines

#!/usr/bin/env python

import os
name = os.getenv('USER')
dir  = os.getcwd()

print "%s ran this from %s" % (name, dir)

Set the file permissions so it is runnable as a command using

$ chmod a+x python-example

Finally, run it using

./python-example

(or use the full path to it; e.g., /home/grundoon/python-example).

Running Python from PBS

Running Python from a PBS script is just like running it in batch, except that you put the python command into a file that is submitted to PBS. For this example, call it test.pbs. (For more information on PBS, see the PBS web page.) Here is an example PBS script to run the hello.py script with Python.

####  PBS preamble
#PBS -N python_test
#PBS -M uniqname@umich.edu
#PBS -m abe

#PBS -l procs=1,mem=1gb
#PBS -j oe
#PBS -V

#PBS -A example_flux
#PBS -l qos=flux
#PBS -q flux

####  End PBS preamble
#  Include the next three lines always
if [ "x${PBS_NODEFILE}" != "x" ] ; then
   cat $PBS_NODEFILE   # contains a list of the CPUs you were using if run with PBS
fi
#  Put your job commands after this line
python hello.py