Bundling data files with PyInstaller (--onefile)

前端 未结 13 1608
清歌不尽
清歌不尽 2020-11-21 13:28

I\'m trying to build a one-file EXE with PyInstaller which is to include an image and an icon. I cannot for the life of me get it to work with --onefile.

<
13条回答
  •  忘掉有多难
    2020-11-21 14:07

    pyinstaller unpacks your data into a temporary folder, and stores this directory path in the _MEIPASS2 environment variable. To get the _MEIPASS2 dir in packed-mode and use the local directory in unpacked (development) mode, I use this:

    def resource_path(relative):
        return os.path.join(
            os.environ.get(
                "_MEIPASS2",
                os.path.abspath(".")
            ),
            relative
        )
    

    Output:

    # in development
    >>> resource_path("app_icon.ico")
    "/home/shish/src/my_app/app_icon.ico"
    
    # in production
    >>> resource_path("app_icon.ico")
    "/tmp/_MEI34121/app_icon.ico"
    

提交回复
热议问题