5

有没有办法让 py2exe 在 library.zip 和/或 exe 文件本身(zipfile=None)中嵌入静态文件(和/或静态文件的子目录),然后在运行时从代码中透明地访问这些嵌入的静态文件?

谢谢你,马尔科姆

4

3 回答 3

4

这听起来像您需要的配方:Extend py2exe to copy files to the zipfile where pkg_resources can load them

有效地使用它可能需要一些与setuptools (部分)相关的pkg_resources知识,“Python Eggs”由此而来。

于 2009-12-15T01:30:43.550 回答
1

只是想我也会在这里分享这个,以帮助那些仍在寻找答案的人:

Py2exe:在 exe 文件本身中嵌入静态文件并访问它们

于 2010-09-10T16:23:17.140 回答
0

不幸的是,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
于 2017-08-22T20:10:44.287 回答