Python

From Alliance Doc
Revision as of 19:05, 17 January 2019 by Mboisson (talk | contribs)
Jump to navigation Jump to search
Other languages:

Description

Python is an interpreted programming language with a design philosophy stressing the readability of code. Its syntax is simple and expressive. Python has an extensive, easy-to-use library of standard modules.

The capabilities of Python can be extended with modules developed by third parties. In general, to simplify operations, it is left up to individual users and groups to install these third-party modules in their own directories. However, most systems offer several versions of Python as well as tools to help you install the third-party modules that you need.

The following sections discuss the Python interpreter, and how to install and use modules.

Loading an interpreter

To discover the versions of Python available:

Question.png
[name@server ~]$ module avail python

You can then load the version of your choice using module load. For example, to load Python 2.7 you can use the command

Question.png
[name@server ~]$ module load python/2.7

SciPy stack

In addition to the base Python module, the SciPy package is also available as an environment module. The scipy-stack module includes:

  • NumPy
  • SciPy
  • Matplotlib
    • dateutil
    • pytz
  • IPython
    • pyzmq
    • tornado
  • pandas
  • Sympy
  • nose

If you want to use any of these Python modules, load a Python version of your choice and then module load scipy-stack.

For more details, including version numbers of the contained packages, visit Scipy.org.

Creating and using a virtual environment

With each version of Python, we provide the tool virtualenv. This tool allows users to create virtual environments within which you can easily install Python modules. These environments allow one to install many versions of the same module, for example, or to compartmentalize a Python installation according to the needs of a specific project. We recommend that you create your Python virtual environment(s) in your home directory.

To create a virtual environment, enter the following command, where ENV is the name of the empty directory containing your environment:

Question.png
[name@server ~]$ virtualenv --no-download ~/ENV

Once the virtual environment has been created, it must be activated:

Question.png
[name@server ~]$ source ~/ENV/bin/activate

To exit the virtual environment, simply enter the command deactivate:

Question.png
[name@server ~]$ deactivate

Installing modules

Once you have a virtual environment loaded, you will be able to run the pip command. This command takes care of compiling and installing most of Python modules and their dependencies. A comprehensive index of Python packages can be found at PyPI.

All of pip's commands are explained in detail in the user guide. We will cover only the most important commands and use the Numpy module as an example.

We first load the Python interpreter:

Question.png
[name@server ~]$ module load python/2.7

We then activate the virtual environment, previously created using the virtualenv command:

Question.png
[name@server ~]$ source ~/ENV/bin/activate

Finally, we install the latest stable version of Numpy:

Question.png
[name@server ~]$ pip install numpy --no-index

The --no-index option could be omitted, but using it guarantees that you will be using a version of numpy that was compiled by the Compute Canada team and can in some cases avoid issues with missing or conflicting dependencies that may arise when pip tries to install a Python package downloaded from the Internet.

If we wanted to install the development version of Numpy, we can also give a link toward its Git repository:

Question.png
[name@server ~]$ pip install git+git://github.com/numpy/numpy.git

In the first invocation of the pip command above it isn't as obvious where the numpy module is being installed from. One might assume it is being installed from PyPI but in the particular case of numpy it is actually being installed from a distribution package offered by Compute Canada. This distribution package is called a python wheel. It will install from Compute Canada's local wheel provided the package version Compute Canada provides is current. If the PyPI has a newer version, that version will be installed instead of the version Compute Canada provides. To disable this default behaviour and use the older Compute Canada specific wheel use the --no-index option.

To see where the pip command is installing a python module from you can tell it to be more verbose with the -vvv option. Compute Canada provides python wheels for many common python modules which are configured to make the best use of the hardware and installed libraries on our clusters.

Creating virtual environments inside of your jobs

Parallel filesystems such as the ones used our clusters are very good at reading or writing large chunks of data, but are rather bad for small chunks of data. Launching a software and loading libraries, such as starting python and loading a virtual environment, is precisely this kind of operation. For this reason, we recommend that you create your virtual environment inside of your job, using the compute node's local disk. It may seem counter-intuitive to recreate your environment for every job, but it is very often much faster than running from the parallel filesystem, and will give you some protection against some filesystem performance issues. This can be achieved using the following submission script example :


File : array_job.sh

#!/bin/bash
#SBATCH --account=def-someuser
#SBATCH --time=1:00:00

module load python/3.6
virtualenv --no-download $SLURM_TMPDIR/env
source $SLURM_TMPDIR/env/bin/activate

pip install --no-index -r requirements.txt
python ...


where the requirements.txt file will have been created from a test environment. For example, if you want to create an environment for Tensorflow, you would do the following on a login node :

[name@server ~]$ module load python/3.6
[name@server ~]$ virtualenv --no-download $ENVDIR
[name@server ~]$ source $ENVDIR/bin/activate
[name@server ~]$ pip install --no-index tensorflow_gpu
[name@server ~]$ pip freeze > requirements.txt
[name@server ~]$ deactivate
[name@server ~]$ rm -rf $ENVDIR


This will yield a file called requirements.txt, with content such as the following

File : array_job.sh

-f /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/avx2
-f /cvmfs/soft.computecanada.ca/custom/python/wheelhouse/generic
absl-py==0.5.0
astor==0.7.1
gast==0.2.0
grpcio==1.17.1
h5py==2.8.0
Keras-Applications==1.0.6
Keras-Preprocessing==1.0.5
Markdown==2.6.11
numpy==1.16.0
protobuf==3.6.1
six==1.12.0
tensorboard==1.12.2
tensorflow-gpu==1.12.0+computecanada
termcolor==1.1.0
Werkzeug==0.14.1


This file will ensure that your environment is reproducible between jobs.

Note that the above instructions require all of the packages you need to be available in the python wheels that we provide (see below). If this is not the case, please contact Technical support to request the wheels you need to be added to our repository.


Listing available wheels

Currently available wheels are listed on the Available Python wheels page. You can also run the command avail_wheels on the cluster. By default, it will:

  • only show you the latest version of a specific package (unless versions are given);
  • only show you versions that are compatible with the python module (if one loaded), otherwise all python versions will be shown;
  • only show you versions that are compatible with the CPU architecture that you are currently running on.

To list wheels containing "cdf" (case insensitive) in its name:

Question.png
[name@server ~]$ avail_wheels --name "*cdf*"
name     version    build    python    arch
-------  ---------  -------  --------  ------
netCDF4  1.4.0               cp27      avx2

Or to list all available versions:

Question.png
[name@server ~]$ avail_wheels --name "*cdf*" --all_version
name     version    build    python    arch
-------  ---------  -------  --------  ------
netCDF4  1.4.0               cp27      avx2
netCDF4  1.3.1               cp36      avx2
netCDF4  1.3.1               cp35      avx2
netCDF4  1.3.1               cp27      avx2
netCDF4  1.2.8               cp27      avx2

Or to list a specific version:

Question.png
[name@server ~]$ avail_wheels --name "*cdf*" --version 1.3
name     version    build    python    arch
-------  ---------  -------  --------  ------
netCDF4  1.3.1               cp36      avx2
netCDF4  1.3.1               cp35      avx2
netCDF4  1.3.1               cp27      avx2

Or to list for a specific version of python:

Question.png
[name@server ~]$ avail_wheels --name "*cdf*" --python 3.6
name     version    build    python    arch
-------  ---------  -------  --------  ------
netCDF4  1.3.1               cp36      avx2

The python column tell us for which python version the wheel is available, where cp36 stands for cpython 3.6.

A few other examples
  • List multiple packages and multiple versions: avail_wheels numpy biopython --version 1.15.0 1.7
  • List the wheels for specific architectures : avail_wheels --arch avx avx2
  • List the wheels specifically for GPU and display only name, version, python columns: avail_wheels --column name version python --all_versions --name "*gpu"
  • Display usage and help: avail_wheels --help

Anaconda

Please see Anaconda.

Jupyter

Please see Jupyter.