Python
======

Python is probably the most versatile programming ecosystem available on the Tardis.
It's an interpreted language that is easy to read, quick to prototype in and comes with a huge number of useful libraries.

Versions
--------

Users need to be aware, that there are still two major versions around that are not 100% compatible to each other.
Although Python 3.0 was released back in 2008, its predecessor Python 2.7 is still around and not all libraries and packages have migrated.
Since the system version on Debian/stable is still 2.7, the :file:`python` program always points to Python 2 (2.7.3).
The :file:`python3` program designates the counterpart (3.2).

The system and core python libraries are, though stable, still quite old (even in the Python 3 branch).
You can use environment modules to switch to a newer version.

Example:

.. code-block:: bash

   # system libraries
   [krause@master ~] python --version
   Python 2.7.3
   [krause@master ~] python3 --version
   Python 3.2.3

   # load new version for python 3
   [krause@master ~] module load python/3.5
   [krause@master ~] python3 --version
   Python 3.5.1

   # load new version for python 2
   [krause@master ~] module unload python
   [krause@master ~] module load python/2.7
   [krause@master ~] python --version
   Python 2.7.12



Packages
--------

Some important packages are already installed system-wide.
To see if they are available, simply try to import them:

.. code-block:: python

   [krause@master ~] python3
   Python 3.5.1 (default, Mar 14 2016, 16:32:54)
   [GCC 4.7.2] on linux
   Type "help", "copyright", "credits" or "license" for more information.
   >>> import lxml
   >>> lxml.__path__
   ['/opt/software/python/3.5/lib/python3.5/site-packages/lxml']


If you happen to need a more recent one or something that hasn't been installed already, the easiest way is to use the default Python package manager :file:`pip`.

Of course it comes separately for Python 2 and 3:

.. code-block:: bash

   [krause@master ~] pip --version
   pip 1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
   [krause@master ~] pip3 --version
   pip 9.0.1 from /home/mpib/krause/.local/lib/python3.5/site-packages (python 3.5)


To install a package in your home directory, you can simply run :program:`pip3 install --user <packagename>`.

Example (install numpy):

.. code-block:: bash

   [krause@master ~] pip3 install --user numpy
   Collecting numpy
     Downloading numpy-1.11.2-cp35-cp35m-manylinux1_x86_64.whl (15.6MB)
       100% |████████████████████████████████| 15.6MB 58kB/s
   Installing collected packages: numpy
   Successfully installed numpy-1.11.2
   [krause@master ~] python3
   Python 3.5.1 (default, Mar 14 2016, 16:32:54)
   [GCC 4.7.2] on linux
   Type "help", "copyright", "credits" or "license" for more information.
   >>> import numpy as np
   >>> np.__version__, np.__path__
   ('1.11.2', ['/home/mpib/krause/.local/lib/python3.5/site-packages/numpy'])


Remember to always install on the tardis master node in case a package needs
special development files which aren't installed on the computation hosts.

.. note::

   The Python Package Index (`PyPI`_), pip's source, contains a lot of user provided, custom and sometimes old and unstable software. Make sure that what you're installing is actually the package that you want. Usually the project's installations notes tell you what the package is called in pypi.

Virtual Environments
--------------------

Sometimes, especially for reproducibility reasons, it may be useful to freeze
the python package versions to a specific release.  You could create a
:file:`requirements.txt` with version numbers and then always run `pip3 install
--user -r requirements.txt`, when you switch a project, but it's way more
convenient to use different python environments for each project.

Starting with Python 3.4 the program `pyvenv`_ will help you manage different environments.
Once created it will copy the current system version's python and pip to a new directory with the environment's name.
Every package installed or upgraded will be contained to that specific directory and you can switch between them very easily.

**Create a new virtual environment**

.. code-block:: bash

   [krause@master ~] module load python/3.5 # global tardis environment
   [krause@master ~] pyvenv .venvs/project  # python environment


**Activate and use the virtual environment**

.. code-block:: bash

   [krause@master ~] source .venvs/project/bin/activate
   (project) [krause@master ~] which python3
   /home/mpib/krause/.venvs/project/bin/python3
   (project) [krause@master ~] which pip3
   /home/mpib/krause/.venvs/project/bin/pip3
   (project) [krause@master ~] pip3 --version
   ppip 7.1.2 from /home/mpib/krause/.venvs/project/lib/python3.5/site-packages (python 3.5)
   (project) [krause@master ~] python3 --version
   Python 3.5.1


**Deactivate the virtual environment**

.. code-block:: bash

  (project) [krause@master ~] deactivate
  [krause@master ~]


Note how :file:`pyvenv` is also managing your shell prompt, so you always know which Python environment you are currently running.

.. _PyPI: https://pypi.python.org/pypi
.. _pyvenv: https://packaging.python.org/installing/#creating-virtual-environments
