Python

Revision as of 19:16, 13 September 2016 by Stubbsda (talk | contribs) (Started to add content for the Python, based on the Calcul Québec page on this same topic.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

Python is an interpreted programing 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:

 
[name@server ~]$ module avail apps/python

You can then load the version of your choice using module load. For example, to load Python 2.7.3 on Colosse:

 
[name@server ~]$ module load apps/python/2.7.10

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, for example, installations of many version of the same modules, or to compartmentalize a Python installation according to the needs of a specific project.

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

 
[name@server ~]$ virtualenv ENV

Note that since Python 3.4.0, virtualenv is integrated with Python and is rather named pyvenv. You must instead use the command

 
[name@server ~]$ pyvenv ENV

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

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

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

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

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:

 
[name@server ~]$ module load apps/python/2.7.10

Note that since Python 3.4.0, virtualenv is integrated with Python and is rather named pyvenv. You must instead use the command

 
[name@server ~]$ pyvenv exp1

The, we activate the virtual environment:

 
[name@server ~]$ source exp1/bin/activate

Finally, we install the latest stable version of Numpy:

 
[name@server ~]$ pip install numpy

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

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