Logo

HPC @ Uni.lu

High Performance Computing in Luxembourg

FAQ: How Do I Install Python Packages?

Installation in your $HOME directory

There are (at least) three possible ways to install a python package in your $HOME directory. All of them have to be followed by a configuration of your environment, more specifically the PYTHONPATH.

1. Install with PIP

PIP is the easiest and recommended way to install Python packages.

$> pip install --user <packagename>

2. Install from source

If the package is not available with PIP, you can install if from source in the following way.

Download the source and unpack it. Change to the source directory.

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

3. Install with easy_install

When using easy_install, you usually have to adjust the PYTHONPATH before the installation. If the directory is not there yet, you have to create it first.

$> mkdir -r $HOME/.local/lib/python2.7/site-packages
$> export PYTHONPATH=$HOME/.local/lib/python2.7/site-packages:$PYTHONPATH

Make sure to insert the right python version if you’re not using 2.7 (see also next section).

$> easy_install --prefix=$HOME/.local <packagename>

Configure your environment

After you installed a python pacakge to your home directory or any other custom location, you need to add that location to your PYTHONPATH. This only has to be done once. The exact path depends on the python version it was installed with. If you followed the instructions above, this path is ~/.local/lib/pythonX.Y/site-packages, where X and Y are the major and minor version of the python you are using (e.g. 2.7 or 3.4). You can check it with python --version. The command to add this location to your environment is

export PYTHONPATH=$HOME/.local/lib/python2.7/site-packages:$PYTHONPATH

(replace 2.7 with your python version). In order to make the change permanent, add this line to your ~/.bash_private (or ~/.bashrc). After this you have to reload the bash configuration.

$> . ~/.bash_private

Virtual environments

In general, Python virtual environments can be really useful to separate different sets of python packages and avoid issues with conflicting versions. They allow you to install packages in isolated locations with easy mechanics to switch between different environments. This is particularly useful if you want to separate packages for different projects or provide a common environment for multiple people.

First, install the virtualenv package with pip like described above and make sure you have added the location to your PYTHONPATH. Then you have to create a directory for your virtual environment and configure it.

$> mkdir <venv_dir>
$> virtualenv <venv_dir>
$> . ./<venv_dir>/bin/activate
$> pip install <python_package>

Replace <venv_dir> with the location where you want your packages to reside and <python_package> with the package name. You can leave the virtualenv with typing

$> deactivate

This brings you back to your usual environment. When you want to use the virtualenv again, you have to activate it with the . ./<venv_dir>/bin/activate command.