UMBC logo
UMBC High Performance Computing Facility
How to run Python on tara

Introduction

On this page we'll see how to use Python on the tara cluster. Before proceeding, make sure you've read the How To Run tutorial first. Python is a popular full-featured scripting language. It can be used interactively or through scripting.

Loading the module

There are several versions of Python installed on tara. To enable the most recent version, enter the following command

[araim1@tara-fe1 ~]$ module load python/2.7.2
This must be done before running your Python code, either interactively or through the batch system. If you forget to load the module, your account will probably default to using an older version.

Alternatively, you can load the module from within a shell script (e.g. from your submission script) as follows

#!/bin/bash -l
#SBATCH --job-name=hello_python
#SBATCH --output=slurm.out
#SBATCH --error=slurm.err
#SBATCH --partition=develop

module load python/2.7.2
./myscript.py
Notice the "-l" argument on the first line, which is necessary. (This allows Bash to use the "module" alias).

Example batch script

We'll write a simple Python script that says "hello", and performs a few calculations.
#!/bin/env python

import math
x = math.factorial(10)
print "Hello world! 10! = %d" % (x)

Download: ../code/python_hello/hello.py
Notice that the first line is not the usual "#!/usr/bin/python", which points to an older version of Python on tara. Instead, "#!/bin/env python" ensures that the module-loaded version of Python is used when invoking the script without the interpreter
[araim1@tara-fe1 python_hello]$ ./hello.py 
Hello world! 10! = 3628800
[araim1@tara-fe1 python_hello]$ 
We can launch it with a standard SLURM script. Again, we'll specify the "-l" option to Bash and load the module from within the batch script. The script will probably function correctly if you haven't made these additions, but have entered the "module load python/2.7.2" command in your current shell session. We suggest these modifications to your batch script regardless, as a best practice.
#!/bin/bash -l
#SBATCH --job-name=hello_python
#SBATCH --output=slurm.out
#SBATCH --error=slurm.err
#SBATCH --partition=develop

module load python/2.7.2
./hello.py

Download: ../code/python_hello/run.slurm
Now we launch the job
[araim1@tara-fe1 python_hello]$ module load python/2.7.2
[araim1@tara-fe1 python_hello]$ sbatch run.slurm
sbatch: Submitted batch job 2618
[araim1@tara-fe1 python_hello]$ ls
hello.py  run.slurm  slurm.err  slurm.out
[araim1@tara-fe1 python_hello]$ cat slurm.out
Hello world! 10! = 3628800
[araim1@tara-fe1 python_hello]$
Also note that hello.py can be run directly on the user node (which is okay here because it's a small job) using the "python" command
[araim1@tara-fe1 python_hello]$ module load python/2.7.2
[araim1@tara-fe1 python_hello]$ python hello.py
Hello world! 10! = 3628800
[araim1@tara-fe1 python_hello]$ 

Running Python interactively

Python can also be used interatively on the user node, as in the following example
[araim1@tara-fe1 ~]$ module load python/2.7.2
[araim1@tara-fe1 ~]$ python
Python 2.7.2 (default, Sep  8 2011, 21:04:00) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> x = math.factorial(10)
>>> print "Hello world! %d" % (x)
Hello world! 10! = 3628800
>>> quit()
[araim1@tara-fe1 ~]$ 
As always, this should only be used for smaller computations. Intensive programs should be submitted to the compute nodes.