Difference between revisions of "Python/Extension"

From ScientificComputing
Jump to: navigation, search
(Created page with "The Python programming language can make use of packages, which extend the functionality of the core language. We provide a central Python package repository, with a range of...")
(No difference)

Revision as of 12:18, 23 August 2016

The Python programming language can make use of packages, which extend the functionality of the core language. We provide a central Python package repository, with a range of installed packages that are available to all cluster users. If a package is not installed centrally, or if you need a newer version than the centrally installed one, then you can also install a Python package locally in your home directory, that can then be used together with the centrally installed Python interpreter.

Installing a Python package locally, using distutils

You can create your personal Python package repository. As an example, we create this directory in $HOME:

mkdir $HOME/python

You are free to choose any other location for your package repository, but it has to contain a given sub-directory structure (which is shown here for python/2.7.6. In case of python/3.3.3, exchange the version number in the directory names from 2.7 to 3.3):

cd $HOME/python
mkdir -p lib64/python2.7/site-packages

In order to make python aware of the local package repository, you should add a line to the $HOME/.bash_profile file:

export PYTHONPATH=$HOME/python/lib64/python2.7/site-packages:$PYTHONPATH

and run this same command on the command line to make your current interactive shell aware of the additional python path. All of the the above steps should be done only once: before you install your first package.

After this preparation, one needs to load the Python module:

module load python/2.7.6

As a next step, you can download and extract the tarball with the source code of the additional package. Go into the directory containing the setup.py file (the directory is usually named package-version). Finally, you can run the command to install the package:

python setup.py install --prefix=$HOME/python

Here the prefix option assures that the package is installed in your local package repository.

With this setup, it is possible to have two versions of a package side-by-side. Python will always search in the locations given in PYTHONPATH first, and if a package is not found there, it will fall back to the central repository.

Installing a Python package, using PIP

Additionally, you can also install a package locally using pip

python -m pip install --user package

The packages will be installed in a local repository at $HOME/.local. Since this is the default path for user-installed python packages, there is no need to adjust PYTHONPATH.

In order to install a particular version of a package, you can use the command

python -m pip install --user package==version

Where "version" denotes the version number that you would like to install.

But be aware that using pip to install a newer version of a package that has originally been installed with easy_install causes problems in the loading order, such that always the version that was installed with easy_install will be loaded unless you change the sys.path in the current Python session.