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.

Thursday, February 26, 2015

pip, virtualenv and virtualenvwrapper-win

I have googled the same information quite often in the past and I would like to collect this information in a post that I can just look up easily. There are lots of tutorials on various aspects of using pip, virtualenv and virtualenvwrapper. This post isn't meant to be a tutorial but rather a reference for certain features/gotchas that fit into my workflow.

An explanatory tutorial video on virtualenv:

An explanatory tutorial video on virtualenvwrapper:

On Windows:

Installing pip

A couple of ways - using easy_install -OR- using the get-pip script

Using easy_install

easy_install is an older package manager that is automatically installed with Python. It is found in <PYTHONHOME>\Scripts. You can just execute

easy_install pip

get_pip script

Download the get-pip script. Once downloaded, you can run at the prompt.

python get-pip.py


To install:

pip install virtualenv


pip install virtualenvwrapper-win

Main commands of virtualenvwrapper-win:

Using a specific python version with a virtual environment

Use the -p or --python argument to point to the python exe that you want the new virtualenv to use

mkvirtualenv <env_name> --python=<path_to_python_exe>

You can also use pywin to switch python versions in an environment

Compiling and linking C/C++ code in a virtual env

pip downloads the source code for a package and if required, kicks off a build using a compatible C/C++ compiler. For additional details on Visual Studio versions used to compile python binaries and consequently, packages visit this link

On Ubuntu:

Installing python, pip, distribute:

sudo apt-get install python
sudo apt-get install python-dev
sudo apt-get install python-setuptools
sudo apt-get install python-pip
sudo easy_install -U distribute

Configuring virtualenvwrapper:

If it doesn't already exist, create ~/.profile and add the following:

$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
$ export WORKON_HOME=~/Envs
$ export PROJECT_HOME=~/Projects
$ source /usr/local/bin/virtualenvwrapper.sh

Ensure that the WORKON_HOME and PROJECT_HOME directories are created. You can either logout and back in -OR- source ~/.profile to update these changes into your environment.

Some important commands:

From the docs,

Managing Environments


Controlling the active environment


Navigating to a virtualenv

  • Change the current working directory to the active virtualenv in $VIRTUAL_ENV

  • Change the current working directory to the site-packages directory of the active virtualenv in $VIRTUAL_ENV


  • lists packages installed in the current virtualenv's sitepackages folder

Path Commands

  • Adds the specified directories to the Python path for the currently-active virtualenv.

Project Directory Management

  • Create a new virtualenv in the WORKON_HOME and project directory in PROJECT_HOME. The names of the virtualenv and the project dir are the same.

  • Binds a directory as the project folder for the current virtualenv
  • Change the current working directory to the one specified as the project directory for the active virtualenv.

Managing installed packages

  • Remove all of the installed third-party packages in the current virtualenv.