Tuesday, July 13, 2010

Compiling Python modules on windows manually

So I wanted to use Pystemmer (http://snowball.tartarus.org/wrappers/PyStemmer-1.1.0.tar.gz not http://sourceforge.net/projects/pystemmer/ which seems to be out of date), but there is no included Visual Studio makefile or solution.

So what to do?

Below are the steps that I find work reasonably well (hopefully documented in a way that will allow other people to do the same for whatever library they are working with).

Note - I assume the following:
  • The code as provided will compile with Visual C (if Visual C cannot compile it - then your job is way harder!)
  • No extra tools (autoconf, etc) are required

So the steps are:
  1. Ensure directory with Python.h is in the %include% directories (menu item: Tools -> Options, Projects and Solutions -> VC++ Directories, Show directories for -> Include Files)
  2. Ensure directory with Python.lib is in the %lib% directories (as as steps for Include files - just select Library Files instead at the end)
  3. In Visual Studio 2008 (for Python 2.6) select menu item File -> New -> Project From Existing Code...
  4. Select the directory with the downloaded code as the Project File Location
  5. Give the project a name (generally a good idea to give it the name of the Python module - i.e. in my example "Stemmer" )
  6. Ensure that the Release build is selected (if using the toolbar) because generally on windows you will not have the debug library: python_d.lib.

  7. Modify the project properties:
    Set General -> Configuration Type to "Dynamic Library (.dll)"
    Set file extenstion for Linker -> General -> Output File to ".pyd"

  8. Try and compile - here are some of the common problems and possible solutions:

    • "multiply defined symbols"

      libstemmer_utf8.obj : error LNK2005: _sb_stemmer_length already defined in libstemmer.obj
      ...
      Release\Stemmer.pyd : fatal error LNK1169: one or more multiply defined symbols found

      The reason for this is that a symbol is defined in 2 source files, for PyStemmer this was because there is both libstemmer_utf8.c and libstemmer.c in the codebase which define the same symbols. The fix was to remove one from the project

    • "Cannot open include file"

      .\src\Stemmer.c(30) : fatal error C1083: Cannot open include file: 'libstemmer.h': No such file or directory

      This is a simple fix - find where the include file is and add it to the include path Project Properties dialog, C/C++ -> General -> Additional Include Directories


This is as much a reminder for me - but if this helps you great :)

Sunday, April 25, 2010

SendKeysCtypes release (for some value of released)

I finally got around to releasing SendKeysCtypes (Sending key strokes to the active app on Windows) to http://bitbucket.org/markm/sendkeysctypes.

There is no setup.py yet, it is not registered on PYPI - but at least it is available.

I did a quick check and had to make a small change to make it work on Python 3.1 :)

Give it a try.

I would like to thank Oliver Rutherford for his sendkeys module (http://www.rutherfurd.net/python/sendkeys/) which I have used for years as part of pywinauto.

I only just realized while writing this that it is also posted to bitbucket http://bitbucket.org/orutherfurd/sendkeys)

The main benefits between this module and Oliver's that I can think of are:

  • No compilation required (works on Python 3)
  • Support for Unicode Characters

Code that works for one of these modules should work for the other also


import SendKeysCtypes
import os, time
os.system("start notepad.exe")
time.sleep(.5)
SendKeysCtypes.SendKeys("Hello in Notepad", with_spaces = True)
SendKeysCtypes.SendKeys("%f") # ALT + F "File" menu
SendKeysCtypes.SendKeys("x") # Exit

Sunday, April 18, 2010

I am speaking at PyCon Asia Pacific in June

I will be speaking at PyCon Asia Pacific.

My talk is titled "Writing Application Plugins in Python". I aim to show how Python can be used to implement plugin functionality for applications that never even heard of Python.

The idea being, that if you write a small amount of shim code (using Pyrex/Cython to make it really easy) then you can pass most of the work off to Python. I work on Windows, and my examples will be on that OS, but very little of what I present will be specific to Windows.

I am still searching around for a good Open Source application with a reasonably simple API/SDK that I can demonstrate with. Leave a comment if you have a suggestion. But the majority of the talk will be generic to any application giving introductions to the various tools and modules used.

If you are in or around Singapoare in June - or feel like seeing South East Asia - then drop by :)

A small library wants to break out of pywinauto

I have been working on pywinauto for about 4 and a half years now and its not really moving in any direction. I make small tweaks here and there and make releases from time to time - but nothing major.

This was the first open source project that I released and I think maybe I was a little over-enthusiastic :). I had hoped that I would be using it myself, I also wished it to be functional and easy enough thatthat it could replace expensive commercial software with similar functionality (but with their own annoying languages). But I am no longer working with said software and my main focus went to other things.

I believe now that pywinauto should be split. There is at least 1 small library (and probobaly more) that should be freed from the overall project.
  • Get information from windows controls (Maybe of the questions on the pywinauto list is how to get the values from text boxes, etc.)
  • A library for automating Windows, not an auotmation framework/language - a library. Meaning that it is up to the user to delay between clicks for windows to open/close. It is up to the user how they want to select controls.
  • SendKeysCtypes (I re-wrote Python SendKeys so that it would support Unicode and not need to be compiled (uses Ctypes). This is more or less ready to release - I just haven't gotten around to doing that :(. (if you need it - grab it from Pywinauto SVN)
  • pywinauto - which would use these previous components but would be much smaller itself, it would work as it does now, trying to help the user as much as possible in not having to worry about timing issues.
Seeing as it turned out that *I* don't really need an automation library/toolkit - it would be great to get some help with some of this work.

Other directions I am thinking of are providing modules so that pywinauto (or the gui automation libarary) can work as an almost direct replacement of things like AutoHotkey or AutoIT or use a similar API as LDTP.

I also need to fix the Pywinauto web presence - which is amazingly scattered - but this post is already long enough.

Sunday, March 07, 2010

Added functions to betterbatch and debugging on the horizon

I was expecting function to be not so hard (just another way of
running a few steps) but it took a little bit of time all the same.

I got it implemented (though no tests for it yet) and it seems OK, I
will know better how it works when I get around to using it (hopefully
in the next week). Weekends for the tool and weekdays for using the
tool in production :)

With this addition I think the language definition is more or less
complete I think. I can imagine tweaking it a bit, fixing bugs etc,
but I don't expect to implement many other language constructs.

One of the next things that I am planning to implement is a simple
debugger (step through statements, print variable value, maybe change
variable values, etc). I was expecting this to be a fairly major
undertaking but again Python and it's community come to the rescue.
Right on cue

a) Catherine Devlin mentioned cmd2 on her blog,
http://catherinedevlin.blogspot.com/2008/01/introducing-cmd2.html
which made me look at cmd in the Python standard library
b) I found the Python documentation a bit dry (or else I had very
little patience) so googled for some examples - and Doug Hellmann's
excellent PyMOTW (http://blog.doughellmann.com/2008/05/pymotw-cmd.html
if you don't know it - bookmark it!) popped up with some good examples
and great documentation.

I found that I was able to throw together a simple debugger (next,
print_var, print_vars, etc)

So this means that a debugger is really easy to write - and is a much smaller task than I was hoping. Maybe next weekend I get the debugger implementation finished.

Tuesday, March 02, 2010

BetterBatch released

I have released BetterBatch (http://code.google.com/p/betterbatch/)
and I would like more people to know about it.

I created it as something that I could use at work. We have many tasks
that require adhoc automations which may have different people running
them. These automations may not have a very long life so it often
doesn't make sense to write it up as Python/Perl script. This work is
done on Windows machines so not many of us (me included) do not have a
lot of experience with shell scripting. Even if we did - shell
scripting wouldn't necessarily be the ideal candidate.

So it was designed as a batch file replacement with some improvements:
Some requirements I had while making this:

  • output from commands can be assigned to variables
  • it needs to be just as easy to call external applications as a batch file
  • all actions should be logged
  • the return value of everything should be checked (and stop the script on error!)
  • it should be prettier than a batch file

I have not tested this on any *nix style machine yet - but believe it
should work (meaning that it probably won't work at first - but I am
hoping that minimal changes will make it work)

Give it a try (please :) )
Download: http://code.google.com/p/betterbatch/downloads/list
Log an issue: http://code.google.com/p/betterbatch/issues/entry
Discuss: http://groups.google.com/group/betterbatch-discuss/topics