Py2exe: Embed static files in library.zip or exe file itself and transparently access them at runtime
Is there a way to have py2exe embed static files (and/or subdirectories of static files) in a librar开发者_StackOverflowy.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
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".
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
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
精彩评论