Tuesday, 1 November 2011

Setting up Python

So far we have looked at creating a home directory with it's own bin, lib and include directories. Now we will cover the last few steps required to get Python working properly.

Assuming Python is already installed on your system, you actually only need to create one directory and a single file. First create a folder called python in your ~/lib folder. Then, in your home directory you can should create a file called .pydistutils.cfg with the following contents:

install_lib = ~/lib/python
install_scripts = ~/bin

What this file does is it tells setuptools that it should put libraries in ~/lib/python and executable commands in ~/bin. Why do we want setuptools to work? Because setuptools is the software suite that includes easy_install which is the way to install python packages.

So try it - run easy_install scikits.learn

So there you go. Now you can install python packages quickly and easily to your home directory (and to remove it you can just run easy_install -mXN sckits.learn and then rm -r ~/lib/python/scikits.learn*)

And what about your own python code? Well, the way I work is to put my source code into my ~/src and then make a symbolic link from ~/lib/python to the packages I want to use. That way my source code is in a sensible place for source control and Python can find it. Also if I want to make some major changes to my code I can replace the symlink with a real copy of my source tree and then keep using the old version of the code while I torture the new version. In fact, probably the "proper" way of working would be to use something like distutils to copy your code from your source tree to ~/lib/python, building any C extensions and running any unit tests at the same time.

I will leave you with one final thought - I think that actually the "really proper and up-to-date" way to do things is to use virtualenv and pip to create a local virtual environment and populate it with python packages, however I haven't had time to look at them yet and I am only writing about things I know. When I've had a chance to look at them I'll update / replace this post with a writeup.