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
1from __future__ import print_function
2from tables import *
3import numarray
4
5class Particle(IsDescription):
6 name = StringCol(16) # 16-character String
7 idnumber = Int64Col() # Signed 64-bit integer
8 ADCcount = UInt16Col() # Unsigned short integer
9 TDCcount = UInt8Col() # Unsigned byte
10 grid_i = Int32Col() # Integer
11 grid_j = IntCol() # Integer (equivalent to Int32Col)
12 pressure = Float32Col() # Float (single-precision)
13 energy = FloatCol() # Double (double-precision)
14
15h5file = openFile("tutorial.h5", mode="w", title="Test file")
16group = h5file.createGroup("/", "detector", "Detector information")
17table = h5file.createTable(group, "readout", Particle, "Readout example")
18
19print(h5file)
20
21particle = table.row
22
23for i in xrange(10):
24 particle['name'] = 'Particle: %6d' % i
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
34table.flush()
35
36table = h5file.root.detector.readout
37pressure = [x['pressure'] for x in table.iterrows() if x['TDCcount']>3 and
38 20<=x['pressure']<50]
39
40print(pressure)
41
42h5file.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!