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

 1import tables as tb
 2
 3
 4class Particle(tb.IsDescription):
 5    name = tb.StringCol(16)  # 16-character String
 6    idnumber = tb.Int64Col()  # Signed 64-bit integer
 7    ADCcount = tb.UInt16Col()  # Unsigned short integer
 8    TDCcount = tb.UInt8Col()  # Unsigned byte
 9    grid_i = tb.Int32Col()  # Integer
10    grid_j = tb.IntCol()  # Integer (equivalent to Int32Col)
11    pressure = tb.Float32Col()  # Float (single-precision)
12    energy = tb.FloatCol()  # Double (double-precision)
13
14
15with tb.open_file("tutorial.h5", mode="w", title="Test file") as h5file:
16    group = h5file.create_group("/", "detector", "Detector information")
17    table = h5file.create_table(group, "readout", Particle, "Readout example")
18
19    print(h5file)
20
21    particle = table.row
22
23    for i in range(10):
24        particle['name'] = f'Particle: {i:6d}'
25        particle['TDCcount'] = i % 256
26        particle['ADCcount'] = (i * 256) % (1 << 16)
27        particle['grid_i'] = i
28        particle['grid_j'] = 10 - i
29        particle['pressure'] = float(i * i)
30        particle['energy'] = float(particle['pressure'] ** 4)
31        particle['idnumber'] = i * (2 ** 34)
32        particle.append()
33
34    table.flush()
35
36with tb.open_file("tutorial.h5", mode="r", title="Test file") as h5file:
37    table = h5file.root.detector.readout
38    pressure = [x['pressure']
39                for x in table.iterrows()
40                if x['TDCcount'] > 3 and 20 <= x['pressure'] < 50]
41
42    print(pressure)

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 setuptools 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.*, numpy.*

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

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> python3 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