Mathieu Fenniak's Weblog

2004/03/12

Distributing a Python Embedding Program

Filed under: programming,python — admin @ 8:39 pm

I got a bit bored at my job yesterday. This happens often, and usually a new random piece of software emerges as a result. This time I ended up hacking on a piece of software that had previously emerged from boredom, which I called the "Difference Machine". I added support for plot datasets generated on the fly through Python code, and in the end learned how to distribute Python within my application.

The purpose of the Difference Machine is very simple: I dislike MS Excel, and wish to never have to use it again. As someone who works in a job involving a lot of engineering, Excel is pretty much standard fare for comparing datasets and stuff of that nature, though. The Difference Machine allows plots generated by the software we develop to be imported, and compared against each other. Additionally, text import is possible from a CSV file or from the clipboard.

differencemachine-plot.png

The Difference Machine comparing two gas supercompressibility (z-factor) correlations.

Adding in the Python embedding was easy. I’ve done similar things before, and the Python C-API is pretty straight-forward (although necessarily verbose). When it came time to put this application up on the network for co-workers to access, the real fun began.

differencemachine-edit.png

The Difference Machine editing Python code with a crude edit control.

My first attempt was simple. Copy the application onto the network, and include python23.dll in the application’s directory. Behold! It actually worked. But, there were a few minor glitches…

  • No access to the standard library. import sys and import math worked, but not import csv or anything else.
  • No standard library meant no traceback library, which I had depended upon to format tracebacks when things went poorly. The software did not react to the missing library well.

I needed the standard library. I tried copying it up into a Lib directory in my application’s directory, and this worked okay… except that it was a huge number of files, and it was still missing things like _sre.pyd, the dynamically linked libraries.

I consulted PEP-237, Import Modules from Zip Archives, to try to create a zip file of the entire standard library that would be much easier to manage than my Lib directory. By naming the zip file python23.zip and putting it in the same directory as my python23.dll, it should have been able to access the library. But it still failed because it couldn’t access any dynamically linked libraries, and zlib is necessary to open the zip file.

By putting all the .pyd files into my application’s directory, I finally got it to work. It loads zlib to read python23.zip, where it retrieves the rest of the standard library. Yippee! In the end, the directory contains the following files:

DifferenceMachine.exe
... bunch of my dlls ...
python23.dll
python23.zip
zlib.pyd
_csv.pyd
_sre.pyd
... bunch more pyd files ...

And all is good and happy. I’m one small step closer to being Excel-less.

3 Comments

  1. py2exe…? It’s really surprisingly easy to use in my very limited personal experience

    http://starship.python.net/crew/theller/py2exe/

    Comment by Ian Bicking — 2012/02/04 @ 5:29 pm

  2. Well, i like PYinstaller(http://pyinstaller.hpcf.upr.edu/cgi-bin/trac.cgi) much than py2exe. And you can find more solution on ‘compile’ a python script in my blog article: http://blender.bokee.com/523381.html

    Yes, I know most of you cannot read Chinese. But you can check the url by your own. ;)

    Comment by Lee June — 2006/01/24 @ 1:35 am

  3. py2exe is great for building a python application into an executable, from what I hear. But in this case, I’m embedding Python into an existing application, and trying to distribute it. I don’t think there’s anything that py2exe can do for me, unless I’m drastically mistaken about what it does.

    Comment by Lao — 2012/02/04 @ 5:29 pm

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.

Powered by WordPress