---
title: "Python Guide"
canonical: "https://kb.uconn.edu/space/SH/26033914173/Python%20Guide"
format: markdown
---
## Loading the Python module

For either method, you must first load the python module. Get a list of available versions using:

```
module avail python
```

For example, to load Python 3.10.5 you would then run:

```
module load python/3.10.5
```

## Submitting Jobs

### Serial

Create a toy Python example script `my_program.py`:

```
print("Hello world")
```

Create the SLURM submission script `submit.sh`:

```
#!/bin/bash
#SBATCH -n 1
python my_program.py
```

### MPI

Please read Laurent Duchesne's excellent step-by-step [guide for parallelizing](http://calculquebec.github.io/cq-formation-advanced-python/ul-20160216/index.html) your Python code using multiple processors and MPI.

On our cluster, to run MPI Python programs, `mpi4py` has been compiled against OpenMPI 1.10.1 therefore we need to load that additional package:

```
module load python/3.4.3 mpi/openmpi/1.10.1-gcc
```

Create the the test MPI example file as described in Laurent's guide above, using the same name `mpi.py`:

```
from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()

print("I am rank", rank, "of", size)
```

Create the SLURM submission script `submit.sh`:

```
#!/bin/bash
#SBATCH -n 4 
mpirun python mpi.py
```

You should get output similar to:

```
I am rank 3 of 4
I am rank 0 of 4
I am rank 1 of 4
I am rank 2 of 4
```

Craig Finch has a more practical example for high throughput MPI on [GitHub](https://github.com/jbornschein/mpi4py-examples/blob/master/09-task-pull.py).

## Installing Python libraries

### Local package install

One can easily install Python packages to your home directory using:

```
python3 -m pip install <name of your package> --user
```

*Alternatively*, one can install miniconda in their home directory to manage python libraries and environments. This is often preferred by Storrs HPC researchers because it allows users to manage different python environments that would normally be incompatible if installed with pip. A full guide on installing miniconda and then using it to manage python libraries can be found [here](https://kb.uconn.edu/space/SH/26079723879/Miniconda+Environment+Set+Up). 

## Jupyter notebooks

A few reasons why you may want to use Jupyter notebooks on the cluster are:

1. Plotting data.
2. Being productive in the same familiar environment as your personal laptop, quickly edit code and preserve the output alongside that code.
3. Integrating functions into a larger reusable script by first interactively using cluster-specific installed python libraries and environmental variables.

> 📝 JupyterHub?

## Summary

### Current Supported Python Versions

3.7.3, 3.10.5

### Check Installed Packages

One can `ls` all the python directories to see a package is installed:

```
# Check which Python versions have numpy installed
ls -d /apps2/python/*/lib/python*/site-packages/numpy

```

If you already have a python module loaded, one can also see all the packages and versions installed with:

```
pip list
```

### Latest Installed Packages List

1. Tensorflow: it already been installed in python 2.7.6. Currently, the cpu version is available. It has been passed our basic testings.

1. scikit-learn (0.17.1)

```
 Notice: you need to load the module "intelics/2012.0.032" before use sklearn 0.17.1. Some functions such as "LinearRegression" depend on some libraries such as libmkl_rt.so which are included by this intel module
```