Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2009-03-18 22:55:29
Size: 1514
Editor: jzinner
Comment:
Revision 4 as of 2013-04-07 18:56:06
Size: 2959
Editor: GrantEdwards
Comment: Add commas between parameters being passed to setup(). You can't use just newlines to separate parameters.
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
Line 8: Line 7:
{{{ {{{#!python
Line 15: Line 14:
    console=['trypyglet.py.py']
    data_files = Mydata_files
    console=['trypyglet.py.py'],
    data_files = Mydata_files,
Line 26: Line 25:
Line 31: Line 29:
{{{ {{{#!python
Line 37: Line 35:
for files in os.listdir('C:\path\to\image\directory\'):
    f1 = 'C:\path\to\image\directory\' + files
for files in os.listdir('C:/path/to/image/directory/'):
    f1 = 'C:/path/to/image/directory/' + files
Line 44: Line 42:
    console=['trypyglet.py.py']
    data_files = Mydata_files
    console=['trypyglet.py.py'],
    data_files = Mydata_files,
Line 55: Line 53:
A helpful function:

{{{#!python
import os
import glob

def find_data_files(source,target,patterns):
    """Locates the specified data-files and returns the matches
    in a data_files compatible format.

    source is the root of the source data tree.
        Use '' or '.' for current directory.
    target is the root of the target data tree.
        Use '' or '.' for the distribution directory.
    patterns is a sequence of glob-patterns for the
        files you want to copy.
    """
    if glob.has_magic(source) or glob.has_magic(target):
        raise ValueError("Magic not allowed in src, target")
    ret = {}
    for pattern in patterns:
        pattern = os.path.join(source,pattern)
        for filename in glob.glob(pattern):
            if os.path.isfile(filename):
                targetpath = os.path.join(target,os.path.relpath(filename,source))
                path = os.path.dirname(targetpath)
                ret.setdefault(path,[]).append(filename)
    return sorted(ret.items())

# Example:
setup(
    name="Sample",
    version="1.0",
    description="A sample app",
    author="MizardX",
    console=['samplescript.py'],
    data_files=find_data_files('data','',[
        'README',
        'images/*',
    ]),
)

# Will copy data/README to dist/README, and all files in data/images/ to dist/images/
# (not checking any subdirectories of data/images/)
}}}

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)