5

Is there a way to have py2exe embed static files (and/or subdirectories of static files) in a library.zip and/or the exe file itself (with zipfile=None) and then transparently access these embedded static files from code at runtime?

Thank you, Malcolm

Malcolm
  • 5,125
  • 10
  • 52
  • 75

3 Answers3

4

This sounds like the recipe you need: Extend py2exe to copy files to the zipfile where pkg_resources can load them

Using that effectively probably requires some knowledge of pkg_resources which is related to (part of) setuptools, whence come "Python Eggs".

Peter Hansen
  • 21,046
  • 5
  • 50
  • 72
1

Just thought I'd share this here too for the benefit of those still looking for answers:

Py2exe: Embed static files in exe file itself and access them

Community
  • 1
  • 1
Darren Bishop
  • 2,379
  • 23
  • 20
0

Unfortunately, py2exe has changed the way that their module works, so the example provided here does not work anymore.

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
Kade
  • 901
  • 13
  • 18