Python Py2exe:将静态文件嵌入library.zip或exe文件本身,并在运行时透明地访问它们
有没有办法让py2exe在library.zip和/或exe文件本身(zipfile=None)中嵌入静态文件(和/或静态文件的子目录),然后在运行时从代码中透明地访问这些嵌入的静态文件 谢谢,,Python Py2exe:将静态文件嵌入library.zip或exe文件本身,并在运行时透明地访问它们,python,py2exe,Python,Py2exe,有没有办法让py2exe在library.zip和/或exe文件本身(zipfile=None)中嵌入静态文件(和/或静态文件的子目录),然后在运行时从代码中透明地访问这些嵌入的静态文件 谢谢,, 马尔科姆这听起来像是你需要的食谱: 有效地使用它可能需要一些与“Python蛋”相关的知识(部分)。我想我也在这里分享一下,以帮助那些仍在寻找答案的人: 不幸的是,py2exe改变了其模块的工作方式,因此提供的示例不再有效 我可以通过重写py2exe的一个函数,然后将它们插入py2exe创建的zipf
马尔科姆这听起来像是你需要的食谱:
有效地使用它可能需要一些与“Python蛋”相关的知识(部分)。我想我也在这里分享一下,以帮助那些仍在寻找答案的人:
不幸的是,py2exe改变了其模块的工作方式,因此提供的示例不再有效 我可以通过重写py2exe的一个函数,然后将它们插入py2exe创建的zipfile中来实现这一点 下面是一个例子:
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
谢谢你,达伦。你好,马尔科姆