How to integrate PyTables in your application by using py2exe

This document shortly describes how to build an executable when using PyTables. Py2exe [1] is a third party product that converts python scripts into standalone windows application/programs. For more information about py2exe please visit http://www.py2exe.org.

To be able to use py2exe you have to download and install it. Please follow the instructions at http://www.py2exe.org.

Let’s assume that you have written a python script as in the attachment py2exe_howto/pytables_test.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from tables import *
import numarray

class Particle(IsDescription):
    name = StringCol(16) # 16-character String
    idnumber = Int64Col() # Signed 64-bit integer
    ADCcount = UInt16Col() # Unsigned short integer
    TDCcount = UInt8Col() # Unsigned byte
    grid_i = Int32Col() # Integer
    grid_j = IntCol() # Integer (equivalent to Int32Col)
    pressure = Float32Col() # Float (single-precision)
    energy = FloatCol() # Double (double-precision)

h5file = openFile("tutorial.h5", mode="w", title="Test file")
group = h5file.createGroup("/", "detector", "Detector information")
table = h5file.createTable(group, "readout", Particle, "Readout example")

print h5file

particle = table.row

for i in xrange(10):
    particle['name'] = 'Particle: %6d' % i
    particle['TDCcount'] = i % 256
    particle['ADCcount'] = (i*256) % (1<<16)
    particle['grid_i'] = i
    particle['grid_j'] = 10 - i
    particle['pressure'] = float(i*i)
    particle['energy'] = float(particle['pressure']**4)
    particle['idnumber'] = i * (2**34)
    particle.append()

table.flush()

table = h5file.root.detector.readout
pressure = [x['pressure'] for x in table.iterrows() if x['TDCcount']>3 and
                                                       20<=x['pressure']<50]

print pressure

h5file.close()

To wrap this script into an executable you have to create a setup script and a configuration script in your program directory.

The setup script will look like this:

from distutils.core import setup
import py2exe
setup(console=['pytables_test.py'])

The configuration script (setup.cfg) specifies which modules to be included and excluded:

[py2exe]
excludes= Tkconstants,Tkinter,tcl
includes= encodings.*, tables.*, numarray.*

As you can see I have included everything from tables (tables.*) and numarray (numarray.*).

Now you are ready to build the executable file (pytable_test.exe). During the build process a subfolder called dist will be created. This folder contains everything needed for your program. All dependencies (dll’s and such stuff) will be copied into this folder. When you distribute your application you have to distribute all files and folders inside the dist folder.

Below you can see how to start the build process (python setup.py py2exe):

c:pytables_test> python setup.py py2exe
...
BUILDING EXECUTABLE
...

After the build process I enter the dist folder and start pytables_test.exe.

c:pytables_test> cd dist

c:pytables_testdist> pytables_test.exe
tutorial.h5 (File) 'Test file'
Last modif.: 'Tue Apr 04 23:09:17 2006'
Object Tree:
/ (RootGroup) 'Test file'
/detector (Group) 'Detector information'
/detector/readout (Table(0,)) 'Readout example'

[25.0, 36.0, 49.0]

DONE!


[1]http://www.py2exe.org