Wednesday, July 1, 2015

Installing PyYAML with LibYAML bindings

PyYAML and LibYAML are libraries that serialize and parse YAML. PyYAML is written in Python while LibYAML is written in C. YAML is a great way to store complex settings, paths to files, etc. The pure python implementation can be slow to load huge YAML files. Though its not a deal breaker, slow load times can get aggravating. The C implementation can speed up loading times significantly.

You can do the following even if you already have PyYAML installed without the LibYAML bindings.

To install PyYAML, get the source code here. Untar the source into a folder <PyYAML_Root>.
Download LibYAML from here and untar the source into <LibYAML_Root>.

Compiling LibYAML

My environment is a Windows 7 box with Python 2.7 and Visual Studio 2008 (VS9.0). Navigate to the following folder and open the solution file with Visual Studio:
Set the configuration to Release and build the solution. You will see that a bin and lib in the Output\Release folder.

Installing PyYAML with LibYAML bindings

Edit the setup.cfg file located in <PyYAML_Root>. At the bottom of the file is present options for include and library path. Uncomment these lines and suitably update to point to :
include: <LibYAML>\include
library: <LibYAML>\win32\vs2008\Output\Release\lib

run at the command line:
python setup.py --with-libyaml install

The build and install should kick off and conclude successfully with the Lib\site-packages folder being updated.

Test PyYAML with LibYAML binding

To validate if the installed PyYAML is linked against LibYAML, run the following

python -c "from yaml import CLoader"

You should be returned to the prompt without incident.