If py2exe includes my data files (e.g. include_package_data)

I have a Python application that includes non-Python data files in some of its subpackages. I used a parameter include_package_datain my own setup.pyto automatically include all these files when creating distributions. It works well.

Now I'm starting to use py2exe. I expected to see what I have include_package_data=Trueand include all the files. But this is not so. It only puts my Python files in library.zip, so my application is not working.

How to get py2exe to include my data files?

+3
source share
4 answers

, py2exe skip_archive=True. Python library.zip, . data_files, Python.

+5

include_package_data - setuptools, distutils. distutils , data_files = []. py2exe - . , glob os.walk . . , ( ), setup.py, , MatPlotLib, py2exe.

, .

+3

, , py2exe .zip. , zip . py2exe .

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

py2exe .

+3

, py2exe, zip , py2exe.

:

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 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

, , , py2exe , . , -.

0

Source: https://habr.com/ru/post/1743576/


All Articles