views:

249

answers:

3

I have a Python app which includes non-Python data files in some of its subpackages. I've been using the include_package_data option in my setup.py to include all these files automatically when making distributions. It works well.

Now I'm starting to use py2exe. I expected it to see that I have include_package_data=True and to include all the files. But it doesn't. It puts only my Python files in the library.zip, so my app doesn't work.

How do I make py2exe include my data files?

A: 

include_package_data is a setuptools option, not a distutils one. In classic distutils, you have to specify the location of data files yourself, using the data_files = [] directive. py2exe is the same. If you have many files, you can use glob or os.walk to retrieve them. See for example the additional changes (datafile additions) required to setup.py to make a module like MatPlotLib work with py2exe.

There is also a mailing list discussion that is relevant.

ire_and_curses
From what I remember, there's a problem with `data_files`: The way you specify the path to which it should install the data files is not relative to where your package is. For example, if you specify `''`, it will copy the data files to `c:\Python26\ `, which is bad. So yeah, you can specify `'Lib\site-packages\whatever'`, but then things will break if the user has installed the package in some other folder.Is there a solution?
cool-RR
I have now tried it, and the problem is different than I remembered it. (Back then I was just installing with `setup.py`, and now we're doing `py2exe` so things are different.) What happens is that the files get put in the `dist` directory, alongside the `.exe`. This is bad because they should be inside the `library.zip` in their natural place inside the various Python packages.How can I get them to be put in the `library.zip`?
cool-RR
A: 

Here's what I use to get py2exe to bundle all of my files into the .zip. Note that to get at your data files, you need to open the zip file. py2exe won't redirect the calls for you.

setup(windows=[target],
      name="myappname",
      data_files = [('', ['data1.dat', 'data2.dat'])],
      options = {'py2exe': {
        "optimize": 2,
        "bundle_files": 2, # This tells py2exe to bundle everything
      }},
)

The full list of py2exe options is here.

Daniel Stutzbach
b-list guy answered my question! I'm honored. But anyway, I wouldn't want to put the data files into the `.exe` file, I'd want them in the Python packages.
cool-RR
@cool-RR I've updated my answer with the settings to bundle everything into the .zip.
Daniel Stutzbach
Wow, didn't know about the `bundle_files` option, thanks. Though I'm starting to like the idea of not having a zip file, so I think I'll stick with that.
cool-RR
+1  A: 

I ended up solving it by giving py2exe the option skip_archive=True. This caused it to put the Python files not in library.zip but simply as plain files. Then I used data_files to put the data files right inside the Python packages.

cool-RR