Python

From oldwiki.scinet.utoronto.ca
Revision as of 11:59, 7 April 2011 by Ljdursi (talk | contribs)
Jump to navigation Jump to search

Python is programing language that continues to grow in popularity for scientific computing. It is very fast to write code in, but the software that results is much much slower than C or Fortran; one should be wary of doing too much compute-intensive work in Python.

At the time of writing, we have two versions of python installed on GPC - the default, version 2.6, and the newest in the 2.7 release series, 2.7.1, in the experimental modules. To use either, you will have to have lines like <source lang=bash> module load gcc python </source> or <source lang=bash> module load gcc use.experimental python/2.7.1 </source>

in your .bashrc.

Many optional packages are available for Python which greatly extend the language adding important new functionality. Those packages which are likely to be important to all of our users — eg, NumPy, SciPy, and Matplotlib are installed system-wide.

Python provides an easy way for users to install the libraries they need in their home directories rather than having them installed system-wide. There are so many optional packages for Python people could potentially want, that we recommend users install these additional packages locally in their home directories. This is almost certainly the easiest way to deal with the wide range of packages, ensure they're up to date, and ensure that users' package choices don't conflict.

To install your own Python modules, follow the instructions below. Where the instructions say python2.X, type python2.6 or python2.7 depending on the version of python you are using.

  • First, create a directory in your home directory, ${HOME}/lib/python2.X/site-packages, where the packages will go.
  • Next, in your .bashrc, *after* you module load python and in the "GPC" section, add the following line:

<source lang=bash> export PYTHONPATH=${PYTHONPATH}:${HOME}/lib/python2.X/site-packages/ </source>

  • Re-load the modified .bashrc by typing source ~/.bashrc.
  • Now, if it's a standard python package and instructions say that you can use easy_intall to install it,
    • install with the following command. where packagename is the name of the package you are installing:

<source lang=bash> easy_install --prefix=${HOME} -O1 [packagename] </source>

    • Continue doing this until all of the packages you need to install are successfully installed.
  • If easy_install isn't an option for your package, and the installation instructions instead talk about downloading a file and using python setup.py install then instead:
    • Download the relevant files
    • You will probably have to uncompress and untar them: tar -xzvf packagename.tgz or tar -xjvf packagename.bz2.
    • cd into the newly created directory, and run

<source lang=bash> python setup.py install --prefix=${HOME} </source>

  • Now, the install process may have added some .egg files or directories to your path. For each .egg directory, add that to your python path as well in your .bashrc, in the same place as you had updated PYTHONPATH before: eg,

<source lang=bash> export PYTHONPATH=${PYTHONPATH}:${HOME}/lib/python2.X/site-packages:${HOME}/lib/python2.X/site-packages/packagename1-x.y.z-yy2.X.egg:${HOME}/lib/python2.X/site-packages/packagename2-a.b.c-py2.X.egg </source>

  • You should now be done! Now, re-source your .bashrc and test your new python modules.
  • In order to keep your .bashrc relatively uncluttered, and to avoid potential conflicts among software modules, we recommend that users create their own modules (for the "module" system, not specifically python modules).

Here is an example module for the Brian package, including instructions for the installation of the python Brian package itself.