Skip to Main Content

How to run Python on maya


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

[jongraf1@maya-usr2 Python]$ python
Python 2.7.2 (default, Jul 10 2014, 19:21:09) 
[GCC 4.6.4 20120303 (prerelease)] 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

To invoke scl packages (in a sub-shell), such as phthon27, 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/

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 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  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 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 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

[araim1@maya-usr1 ~]$ 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! 3628800
>>> quit()
[araim1@maya-usr1 ~]$ 

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