Saturday, October 16, 2010

How to Install Lettuce in Windows

Lately I've been working in python. It is my first experience with the language and as part of the learning curve I should not only learn the nuts and bolts of the language but also research the tools I will use.
Lettuce is the tool I’ve chosen for BDD but it wasn’t easy to start working with it. I’ll share the steps necessary for a proper installation and configuration in a Windows environment.
These steps were performed in Windows 2003 server and I’m assuming that you have Python installed and configured as follows:

1. Installation

The first issue appeared during the installation process. In lettuce’s website they use the pip command but I was unable to run lettuce after using this so I decided to try easy_install which worked for me:
easy_install lettuce
After the installation, if you try to run lettuce in your command prompt you will see that lettuce is not found.

This happens because the lettuce file is not an executable, therefore you need to tweak this a little bit.

2. Execution

The lettuce file has no extension and is located in the Scripts folder of your python installation, in my case the file is in the folder C:\Python26\Scripts.
Because in Windows any .py file will be executed using the python interpreter, a quick fix could be to add that extension to the file, and then call lettuce using the extension instead.
After this you will see that the file is executed but an error is reported.

This happens because after renaming the file to python will use "lettuce" as a module name, hiding another lettuce module needed for a proper execution. A fix for this problem is to rename this file to something else, for instance:

Great! We don't have errors any more, lettuce is running but, what just happening here with these weird characters?
Note: If adding the python extension is an issue for you, you can create an .exe from your .py file. Windows will be able to find it and execute it. I recommend the exemaker tool which is pretty simple to use.

3. Fixing the Output

Lettuce is using colors in the output and is implemented by sending ANSI escape characters to the terminal, a feature that works in Unix and Mac, but not in Windows.
Fortunately there is a Python package that allows us to translate those ANSI escape characters into colors, colorama. First you need to install it:
easy_install colorama
When the installation finishes you have to tell colorama to translate lettuce’s output. We will do this by modifying the lettuce package initializer In my case this file is located in the folder: C:\Python26\Lib\site-packages\lettuce-0.1.15-py2.6.egg\lettuce
Add the following code after the latest import clause:
from colorama import init init()
This is how the modification looks

After this change, run lettuce again and you will see the colored output.

Happy lettucing!