The data_files allows you to include one (or more) series of files that you are going to need to run your executable. For example, to run a GUI executable, I use data_files to create a folder in my dist directory called "images" and copy my list of image files over.

The data_files takes a tuple 1) the location to store the data and 2) the location to copy the data from

Example:

   1 from distutils.core import setup
   2 import py2exe
   3 
   4 Mydata_files = [('images', ['c:/path/to/image/image.png'])]
   5 
   6 setup(
   7     console=['trypyglet.py.py'],
   8     data_files = Mydata_files,
   9     options={
  10                 "py2exe":{
  11                         "unbuffered": True,
  12                         "optimize": 2,
  13                         "excludes": ["email"]
  14                 }
  15         }
  16 )

More often you'll have a directory of image files, and you can use some code to push the directory into a list:

Example:

   1 import os
   2 from distutils.core import setup
   3 import py2exe
   4 
   5 Mydata_files = []
   6 for files in os.listdir('C:/path/to/image/directory/'):
   7     f1 = 'C:/path/to/image/directory/' + files
   8     if os.path.isfile(f1): # skip directories
   9         f2 = 'images', [f1]
  10         Mydata_files.append(f2)
  11 
  12 setup(
  13     console=['trypyglet.py.py'],
  14     data_files = Mydata_files,
  15     options={
  16                 "py2exe":{
  17                         "unbuffered": True,
  18                         "optimize": 2,
  19                         "excludes": ["email"]
  20                 }
  21         }
  22 )

A helpful function:

   1 import os
   2 import glob
   3 
   4 def find_data_files(source,target,patterns):
   5     """Locates the specified data-files and returns the matches
   6     in a data_files compatible format.
   7 
   8     source is the root of the source data tree.
   9         Use '' or '.' for current directory.
  10     target is the root of the target data tree.
  11         Use '' or '.' for the distribution directory.
  12     patterns is a sequence of glob-patterns for the
  13         files you want to copy.
  14     """
  15     if glob.has_magic(source) or glob.has_magic(target):
  16         raise ValueError("Magic not allowed in src, target")
  17     ret = {}
  18     for pattern in patterns:
  19         pattern = os.path.join(source,pattern)
  20         for filename in glob.glob(pattern):
  21             if os.path.isfile(filename):
  22                 targetpath = os.path.join(target,os.path.relpath(filename,source))
  23                 path = os.path.dirname(targetpath)
  24                 ret.setdefault(path,[]).append(filename)
  25     return sorted(ret.items())
  26 
  27 # Example:
  28 setup(
  29     name="Sample",
  30     version="1.0",
  31     description="A sample app",
  32     author="MizardX",
  33     console=['samplescript.py'],
  34     data_files=find_data_files('data','',[
  35         'README',
  36         'images/*',
  37     ]),
  38 )
  39 
  40 # Will copy data/README to dist/README, and all files in data/images/ to dist/images/
  41 # (not checking any subdirectories of data/images/)

data_files (last edited 2013-04-07 18:56:06 by GrantEdwards)