python.rst 6.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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.
12
13
Since the system version on Debian/stable is still 2.7, the :file:`python` program always points to Python 2 (2.7).
The :file:`python3` program designates the counterpart (3.5).
14
15
16
17
18
19
20
21
22
23

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
24
   Python 2.7.13
25
   [krause@master ~] python3 --version
26
   Python 3.5.3
27
28

   # load new version for python 3
29
   [krause@master ~] module load python/3.6
30
   [krause@master ~] python3 --version
31
   Python 3.6.3
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58



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
59
   pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
60
   [krause@master ~] pip3 --version
61
62
   pip 9.0.1 from /opt/software/python/3.6.3/lib/python3.6/site-packages (python 3.6)

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104


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.
105
For convenience reasons we also installed a module called `virtualenvwrapper`_, which provides three important commands to handle environments: `mkvirtualenv`, `workon`, and `deactivate`.
106
107
108
109
110

**Create a new virtual environment**

.. code-block:: bash

Michael Krause's avatar
Michael Krause committed
111
   [krause@master ~] mkvirtualenv --python=$(which python3) project
112
   (project) [krause@master ~]
113
114
115
116
117
118


**Activate and use the virtual environment**

.. code-block:: bash

119
120
121
122
123
124
125
126
127

   # without virtual environment
   [krause@master ~] python --version
   Python 2.7.13

   # with virtual environment
   [krause@master ~] workon project
   (project) [krause@master ~] python --version
   Python 3.5.3
128
   (project) [krause@master ~] which python3
129
   /home/mpib/krause/.virtualenvs/project/bin/python
Michael Krause's avatar
Michael Krause committed
130
   (project) [krause@master ~] pip --version
131
   pip 9.0.1 from /home/mpib/krause/.virtualenvs/project/lib/python3.5/site-packages (python 3.5)
Michael Krause's avatar
Michael Krause committed
132
133
134
135
136
137
   (project) [krause@master ~] pip install numpy
   Collecting numpy
   Using cached https://files.pythonhosted.org/packages/fe/94/7049fed8373c52839c8cde619acaf2c9b83082b935e5aa8c0fa27a4a8bcc/numpy-1.15.1-cp35-cp35m-manylinux1_x86_64.whl
   Installing collected packages: numpy
   Successfully installed numpy-1.15.1

138
139
140
141
142
143
144
145
146
147


**Deactivate the virtual environment**

.. code-block:: bash

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


148
149
150
151
152
153
154
155
156
157
158
159
Note how :file:`virtualenv` is also managing your shell prompt, so you always
know which Python environment you are currently running. All your virtual
environments created this way reside in your home directory under
:file:`~/.virtualenvs/`. In theory you could just run the virtual python
interpreter that is installed in :file:`~/.virtualenvs/<env_name>/bin/python`
directly. It is much more convenient to use the wrapper functions though.

.. important::

   To use the virtualenvwrapper convenience functions (workon etc) in a torque job file you need to add one of the following lines to your job definitions:
   :file:`source /etc/bash_completion` **or** :file:`module load virtualenvwrapper`

160
161

.. _PyPI: https://pypi.python.org/pypi
162
163
.. _pyvenv: https://virtualenvwrapper.readthedocs.io/en/latest
.. _virtualenvwrapper: https://packaging.python.org/installing/#creating-virtual-environments