Alexandre M. Savio
Published

Sat 26 April 2014

←Home

Installing common Python data analysis tools in virtualenv

Tested on Ubuntu 13.10 and 14.04.

Requirements

sudo apt-get install python-pip python-dev

sudo pip install virtualenv virtualenvwrapper

echo "export WORKON_HOME=~/envs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
echo "export PIP_REQUIRE_VIRTUALENV=true" >> ~/.bashrc
source ~/.bashrc

Create a virtual environment

mkvirtualenv <env_name> -p <path_to_python>*

*<path_to_python> can be either /usr/bin/python2 or /usr/bin/python3,
or whatever you need.

Install modules

IPython

pip install ipython[all]

Numpy

pip install numpy

Test numpy

pip install nose

python -c 'import numpy; numpy.test()'

Cython

pip install cython

Scipy

sudo apt-get build-dep python-scipy

sudo apt-get install libblas-dev liblapack-dev

pip install scipy

Readline

sudo apt-get install libncurses5-dev

pip install readline

Other dependencies

pip install python-dateutil sphinx pygments tornado

Graphical libraries for GUI building and IPython QT console

You can either install them inside the virtual environment:

 pip install pyside

You must install PyQt4 globally:

 sudo apt-get install python3-pyqt4 python3-sip python-qt4-dev

or

 sudo apt-get install python-pyqt4 python-sip python-qt4-dev

I recommend installing them globally then linking from the virtual environment:

First install them:

sudo apt-get install python3-pyqt4 python3-pyside python3-sip

or

sudo apt-get install python-qt4 python-pyside python-sip python-qt4-dev

Then link them (with the virtualenv activated):

In [3] we can see a script to link any library to the virtual environment (save it in, e.g. symlink_pyqt4_and_sip.sh or ${WORKON_HOME}/postmkvirtualenv and execute it):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
# This hook is executed after a new virtualenv is activated.
# ~/.virtualenvs/postmkvirtualenv

function find_real_lib {
    syspy=$1
    libname=$2
    syspath=$($syspy -c "import sys; print(' '.join(sys.path).strip())")
    for libdir in $syspath; do
        if [ -e $libdir/$libname ]; then
            eval "$3=$libdir/$libname"
            return 0
        fi
    done
    return 1
}

libs=( PyQt4 sip.so PySide sip.cpython-34m-x86_64-linux-gnu.so )
#other_libs = ( vtk cairo )

python_version=python$(python -c "import sys; print (str(sys.version_info[0])+'.'+str(sys.version_info[1]))")
var=( $(which -a $python_version) )
syspy=${var[-1]}

get_python_lib_cmd="from distutils.sysconfig import get_python_lib; print (get_python_lib())"
lib_virtualenv_path=$(python -c "$get_python_lib_cmd")

for lib in ${libs[@]}
do
    libsyspath=''
    find_real_lib $syspy $lib libsyspath
    if [ $? == '0' ]; then
        ln -s $libsyspath $lib_virtualenv_path/$lib
    fi
done

Other interesting modules

matplotlib

sudo apt-get build-dep python-matplotlib

pip install pyparsing

pip install matplotlib

scikit-image

pip install scikit-image

scikit-learn

pip install scikit-learn

scikit-fuzzy

Pandas

sudo apt-get install libhdf5-dev

pip install numexpr
pip install tables

If you have problems importing tables, it might help downloading and compiling the HDF5 library.

pip install pandas

h5py

sudo apt-get install libhdf5-dev

pip install h5py

patsy and statsmodels

pip install patsy
pip install statsmodels

pymc

pip install pymc

Speed up execution

bottleneck

pip install bottleneck

cythongsl cythongsl-demo

pip install cythongsl

Neuroscience tools:

Nibabel

pip install nibabel

Nipy Dependencies:

pip install sphinx sympy networkx traits

pip install nipy

pip install nipype dipy nitime

References

  1. http://www.cs.dartmouth.edu/~nfoti/blog/blog/2012/07/17/setting-up- virtualenv-for-data-analysis-on-osx/

  2. http://calvinx.com/2012/11/03/ipython-qtconsole/

  3. https://gist.github.com/alexsavio/7580457

Go Top