Technical Leader

xmllib2 and python

I have been working with Python and libxml2 for a bit now and have found out there is little documentation out there on how to get everything working together. Also as I have very little permissions on some of the machine I work on, I need to be able to have my own installation of Python without affecting the entire system. I have put together the following steps to follow for installing your own installation of Python using least privileges (without root access) for both Windows and Linux.

One quick note about Linux, a lot of GNU Linux distributions out there might already have Python2.5, but I have not had the luck of dealing with any of these. You should check to make sure you need to install a custom installation.

Typically I would like the latest and greatest of a package, however; the libxml2 Windows package does not seem to acknowledge Python2.6 or above, so to keep everything on the same page I use Python2.5 on Linux as to match up with a Windows version also. It goes without saying, but if platform independence doesn’t matter to you then choose whichever way you would like, for myself I like to have my scripts be able to run on Linux and Windows.

Download and extract Python-2.5.2.tar.bz2 from http://www.python.org/ftp/python/2.5.2/. I typically just work out of my home directory, but to each their own.

wget http://www.python.org/ftp/python/2.5.2/Python-2.5.2.tar.bz2

Now just a simple extraction:

tar xjf Python-2.5.2.tar.bz2

cd Python-2.5.2

Now we need to configure the python setup. The key part is the –prefix portion, as many people out there are not too familiar with building a source distribution by hand. This basically is the location where all the binaries, libraries, and man pages will be stored. The layout under this directory will be something similar to (I kept this brief, you will have more than this):

$HOME/utils
  +- include/
       |- python2.5
  +- lib/
       |- python2.5
  +- share/
       |- man
  +- bin/
       |- python
       |- pydoc
       |- python2.5
export LDFLAGS="-R$HOME/utils/lib"

./configure --prefix=$HOME/utils

make

I use the LDFLAGS variable to keep from dealing with LD_LIBRARY_PATH, please these links for more information.

Note: On some hosts, such as Site5, you will need to ask IT Support to be put into a compiler group in order to obtain access to gcc.

Now we will install all the built binaries and libraries into the prefix directory we specified earlier.

make install

I will not get into it here, but it is possible to install multiple versions of Python side-by-side. If this is something you would like, then look into using make altinstall instead. The README file details this in the Installing section (around 75% into the file).

If all goes well, then $PREFIX/bin/python should work. I typically adjust my PATH environment variable to accommodate this:

export PATH=$HOME/utils/bin:$PATH

Installing libxml2 on Linux

As these steps follow extremely close to the preceding steps, I will make this a little more brief.

Download libxml2-2.7.2.tar.gz from ftp://xmlsoft.org/libxml2/ (a newer version might be available).

Extract the package in to a working directory.

tar xzf libxml2-2.7.2.tar.gz

cd libxml2-2.7.2

Then execute the following:

./configure --prefix=$HOME/utils --with-python=$HOME/utils/bin/python

make

make install

Installing libxml2 on Windows

To get libxml2 working on Windows, it is as trivial as downloading a package from http://users.skynet.be/sbi/libxml-python/, it automatically detects an installation of python and installs itself.

In some future posts, I’ll go over using libxml2 with python.