Bundling data files with PyInstaller (--onefile)

前端 未结 13 1586
清歌不尽
清歌不尽 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:11

    i use this based on max solution

    def getPath(filename):
        import os
        import sys
        from os import chdir
        from os.path import join
        from os.path import dirname
        from os import environ
        
        if hasattr(sys, '_MEIPASS'):
            # PyInstaller >= 1.6
            chdir(sys._MEIPASS)
            filename = join(sys._MEIPASS, filename)
        elif '_MEIPASS2' in environ:
            # PyInstaller < 1.6 (tested on 1.5 only)
            chdir(environ['_MEIPASS2'])
            filename = join(environ['_MEIPASS2'], filename)
        else:
            chdir(dirname(sys.argv[0]))
            filename = join(dirname(sys.argv[0]), filename)
            
        return filename
    

提交回复
热议问题