Having py2exe include my data files (like include_

2019-02-19 05:39发布

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?

4条回答
▲ chillily
2楼-- · 2019-02-19 05:47

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.

查看更多
虎瘦雄心在
3楼-- · 2019-02-19 05:50

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-02-19 05:50

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.

查看更多
何必那么认真
5楼-- · 2019-02-19 05:56

I have been able to do this by overriding one of py2exe's functions, and then just inserting them into the zipfile that is created by py2exe.

Here's an example:

import py2exe
import zipfile

myFiles = [
    "C:/Users/Kade/Documents/ExampleFiles/example_1.doc",
    "C:/Users/Kade/Documents/ExampleFiles/example_2.dll",
    "C:/Users/Kade/Documents/ExampleFiles/example_3.obj",
    "C:/Users/Kade/Documents/ExampleFiles/example_4.H",
    ]

def better_copy_files(self, destdir):
    """Overriden so that things can be included in the library.zip."""

    #Run function as normal
    original_copy_files(self, destdir)

    #Get the zipfile's location
    if self.options.libname is not None:
        libpath = os.path.join(destdir, self.options.libname)

        #Re-open the zip file
        if self.options.compress:
            compression = zipfile.ZIP_DEFLATED
        else:
            compression = zipfile.ZIP_STORED
        arc = zipfile.ZipFile(libpath, "a", compression = compression)

        #Add your items to the zipfile
        for item in myFiles:
            if self.options.verbose:
                print("Copy File %s to %s" % (item, libpath))
            arc.write(item, os.path.basename(item))
        arc.close()

#Connect overrides
original_copy_files = py2exe.runtime.Runtime.copy_files
py2exe.runtime.Runtime.copy_files = better_copy_files

I got the idea from here, but unfortunately py2exe has changed how they do things sense then. I hope this helps someone out.

查看更多
登录 后发表回答