py2exe : library.zip 또는 exe 파일 자체에 정적 파일을 포함시키고 런타임에 투명하게 액세스하십시오.

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

  •  19-09-2019
  •  | 
  •  

문제

py2exe가 library.zip 및/또는 exe 파일 자체 (zipfile = none)에 정적 파일 (및/또는 정적 파일의 하위 디렉토리)을 포함시키는 방법이 있습니까?

감사합니다, Malcolm

도움이 되었습니까?

해결책

이것은 필요한 레시피처럼 들립니다. py2exe를 연장하여 pkg_resources를로드 할 수있는 zipfile에 파일을 복사하십시오.

그것을 효과적으로 사용하려면 아마도 약간의 지식이 필요할 것입니다 pkg_resources (일부)와 관련이 있습니다. SetUptools, "Python Eggs"가 올 때.

다른 팁

여전히 답을 찾고있는 사람들의 이익을 위해 여기에서 이것을 공유 할 것이라고 생각했습니다.

py2exe : exe 파일 자체에 정적 파일을 포함시키고 액세스하십시오.

불행히도 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top