有没有办法让py2exe在library.zip和/或exe文件本身(用zipfile = None)中嵌入静态文件(和/或静态文件的子目录),然后从代码中透明地访问这些嵌入的静态文件在运行时?
谢谢你, 马尔科姆
答案 0 :(得分:4)
这听起来像你需要的食谱:Extend py2exe to copy files to the zipfile where pkg_resources can load them
有效地使用它可能需要一些pkg_resources的知识,这些知识与({3}}的(部分)有关,从而出现“Python Eggs”。
答案 1 :(得分:1)
以为我也会在这里分享这个,为了那些仍在寻找答案的人的利益:
Py2exe: Embed static files in exe file itself and access them
答案 2 :(得分:0)
不幸的是,py2exe改变了模块的工作方式,因此提供的示例here不再起作用。
我能够通过覆盖py2exe的一个函数,然后将它们插入由py2exe创建的zip文件中来实现。
以下是一个例子:
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