5

py2exe に静的ファイル (および/または静的ファイルのサブディレクトリ) を library.zip および/または exe ファイル自体 (zipfile=None) に埋め込み、実行時にコードからこれらの埋め込まれた静的ファイルに透過的にアクセスする方法はありますか?

ありがとう、マルコム

4

3 に答える 3

4

これは必要なレシピのように思えます: py2exe を拡張して、pkg_resources がファイルをロードできる zip ファイルにファイルをコピーします

それを効果的に使用するには、おそらくsetuptools (の一部) に関連するpkg_resourcesの知識が必要であり、そこから「Python Eggs」が生まれます。

于 2009-12-15T01:30:43.550 に答える
1

まだ答えを探している人のために、ここでもこれを共有したいと思いました:

Py2exe:exeファイル自体に静的ファイルを埋め込んでアクセスする

于 2010-09-10T16:23:17.140 に答える
0

残念ながら、py2exe はモジュールの動作方法を変更したため、ここで提供されている例は動作しなくなりました。

これは、py2exe の関数の 1 つをオーバーライドしてから、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
于 2017-08-22T20:10:44.287 に答える