Py2exe:嵌入静态的文件library.zip 或文件本身和透明的访问它们在运行时

StackOverflow https://stackoverflow.com/questions/1904647

  •  19-09-2019
  •  | 
  •  

有没有办法有py2exe嵌入静态的文件(和/或子目录的静态的文件)中的一个library.zip 和/或文件本身(与出压缩文件=None)和然后透明地接入这些嵌入式静态的文件码在运行时?

谢谢你, 马尔科姆

有帮助吗?

解决方案

这听起来像您需要的食谱:扩展py2exe将文件复制到zip文件,其中通过pkg_resources可以加载它们

使用,有效地可能需要有一定的了解通过pkg_resources 这与(部分) setuptools的时,那里来的 “Python的鸡蛋”。

其他提示

只是想我会在这里分享这也为那些仍在寻找答案的好处:

Py2exe:嵌入在EXE文件本身并访问他们静态文件

不幸的是,py2exe已经改变了他们的模块的工作,所以提供的示例 在这里, 不工作了。

我已经能够这样做的压倒一切的一py2exe的职能,然后只是插入到他们的出压缩文件的创建的py2exe.

这里有一个例子:

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top