Category Archives: Python

Installing the Enthought Python Distribution with OpenCV and Theano

This post describes how to setup the Enthought Python Distribution (ETD) without the need for root privileges in a Debian-based Linux distribution (tested on Debian unstable and Ubuntu 10.04).
This is an easy way to get a working Python environment without having to deal with dependencies and out-dated packages in the repositories.

Also we’re going to compile OpenCV and add its Python bindings to our ETD environment.

Enthought Python Distribution (EPD)

There is a free release of the Enthought Python Distribution , but since I have access to the academic version (available to “students and staff at degree-granting institutions”), which ships with a lot of additional packages useful for scientific computing, I’ve never tried it and can’t say whether additional steps need to be taken.

After downloading a copy of EPD, you can install it by running the following command from the download directory (adjust the filename for different versions).

$ ./epd-7.3-2-rh5-x86_64.sh

After accepting the license terms, the installer will ask for the install path, which I’ve set to $HOME/.local/epd-7.3-2-rh5-x86_64. Then it starts some tests and compiles the Python modules.
When it is finished, the following lines need to be added to ~/.bashrc. If you didn’t use $HOME/.local/epd-7.3-2-rh5-x86_64 as install path, you have to adjust the EPDPATH line accordingly.

export EPDPATH=$HOME/.local/epd-7.3-2-rh5-x86_64
# prepend the EPD bin dir to your path to make your shell prefer the EPD python interpreter to the system python interpreter
export PATH=$EPDPATH/bin:$PATH
# also the python packages should be searched in the correct location
export PYTHONPATH=$EPDPATH/lib/python2.7/site-packages

After saving and closing ~/.bashrc type the following command, so the environment variables are set correctly for the OpenCV installation

source ~/.bashrc

OpenCV

Download the latest release of OpenCV from http://sourceforge.net/projects/opencvlibrary/files/

Extract the downloaded archive:

tar xjfv OpenCV-2.4.3.tar.bz2
cd OpenCV-2.4.3

Now run cmake from a newly created build directory. You have to specify some flags so the Python bindings will later be built and installed for your installation of EPD. (Jan 11th 2013: fixed the cmake command, PYTHON_INCLUDE_DIR:PATH flag should be set to $EPDPATH/include/python2.7 not $EPDPATH/include/python)

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX:PATH=$HOME/.local -D PYTHON_EXECUTABLE:FILEPATH=$EPDPATH/bin/python -D PYTHON_INCLUDE_DIR:PATH=$EPDPATH/include/python2.7 -D PYTHON_LIBRARY:FILEPATH=$EPDPATH/lib/libpython2.7.so -D PYTHON_LIBRARY_PATH:UNINITIALIZED=$EPDPATH/lib/libpython2.7.so -D PYTHON_NUMPY_INCLUDE_DIR:PATH=$EPDPATH/lib/python2.7/site-packages/numpy/core/include -D PYTHON_PACKAGES_PATH:PATH=$EPDPATH/lib/python2.7/site-packages -D SPHINX_BUILD:FILEPATH=$EPDPATH/bin/sphinx-build -D PYTHONINTERP_FOUND=1 -D BUILD_PYTHON_SUPPORT=ON -D INSTALL_PYTHON_EXAMPLES:BOOL=ON ..

If there are no error messages, you should now be able to run

make
make install

to build and install OpenCV

If all went well, the following two commands should display the location of the python executable inside your install directory and the OpenCV version and location of the cv2 module

python --version
python -c "import cv2;print 'OpenCV version: {0} (module file: {1})'.format(cv2.__version__, cv2.__file__)"

Theano

To add the Theano package to your EPD environment, first install pip, a tool to install packages from the Python Package Index:

easy_install pip

and then run

pip install Theano