Skip to content Skip to sidebar Skip to footer

Having Py2exe Include My Data Files (like Include_package_data)

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 autom

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

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",
    ]

defbetter_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 locationif self.options.libname isnotNone:
        libpath = os.path.join(destdir, self.options.libname)

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

        #Add your items to the zipfilefor 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.

Post a Comment for "Having Py2exe Include My Data Files (like Include_package_data)"