How to run Python on maya

Introduction

On this page we’ll see how to use Python on the maya 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.

Change version

There are several versions of Python installed on maya. To use the default version, just enter python

[jcorn2@maya-usr1 ~]$ python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

To list all the scl packages available, use the following command:

[jongraf1@maya-usr2 Python]$ scl -l
python27
python33
ruby200

To invoke scl packages (in a sub-shell), such as python27, use:

[hu6@maya-usr1 ~]$ scl enable python27 bash

Then when you enter python, you can see the version has been changed:

[hu6@maya-usr1 ~]$ python
Python 2.7.5 (default, Dec  3 2013, 08:35:16) 

To directly run a command that requires a specific version of python:

[hu6@maya-usr1 ~]$ scl enable python27 'command'

Example batch script

We’ll write a simple Python script that says “hello”, and performs a few calculations.


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 maya. Instead, “#!/bin/env python” ensures that the module-loaded version of Python is used when invoking the script without the interpreter. Make sure that the permission for the file include the ability to execute.

[araim1@maya-usr1 python_hello]$ ./hello.py 
Hello world! 10! = 3628800
[araim1@maya-usr1 python_hello]$ 

We can launch it with a standard SLURM script.

Download: ..code/python_hello/run.slurm

Now we launch the job

[araim1@maya-usr1 python_hello]$ sbatch run.slurm
sbatch: Submitted batch job 2618
[araim1@maya-usr1 python_hello]$ ls
hello.py  run.slurm  slurm.err  slurm.out
[araim1@maya-usr1 python_hello]$ cat slurm.out
Hello world! 10! = 3628800
[araim1@maya-usr1 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@maya-usr1 python_hello]$ python hello.py
Hello world! 10! = 3628800
[araim1@maya-usr1 python_hello]$ 

Running Python interactively

Python can also be used interatively on the user node, as in the following example

[jcorn2@maya-usr1 ~]$ python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> x = math.factorial(10)
>>> print "Hello world! %d" % (x)
Hello world! 3628800
>>> quit()
[jcorn2@maya-usr1 ~]$ 

As always, this should only be used for smaller computations. Intensive programs should be submitted to the compute nodes.