Py2exe: Embed static files in exe file itself and access them

前端 未结 3 944
名媛妹妹
名媛妹妹 2020-12-06 06:34

I found a solution to add files in library.zip via: Extend py2exe to copy files to the zipfile where pkg_resources can load them.

I can access to my file when library

3条回答
  •  旧巷少年郎
    2020-12-06 07:04

    You shouldn't be using pkg_resources to retrieve the library.zip file. You should use it to retrieve the added resource.

    Suppose you have the following project structure:

    setup.py
    foo/
        __init__.py
        bar.py
        media/
            image.jpg
    

    You would use resource_string (or, preferably, resource_stream) to access image.jpg:

    img = pkg_resources.resource_string(__name__, 'media/image.jpg')
    

    That should "just work". At least it did when I bundled my media files in the EXE. (Sorry, I've since left the company where I was using py2exe, so don't have a working example to draw on.)

    You could also try using pkg_resources.resource_filename(), but I don't think that works under py2exe.

提交回复
热议问题