How To Include Images In One File With Python Pyinstaller
I have image locations included in my python code but when I compile to an exe with pyinstaller using one file and all of the --add-data commands, the exe wont run stating that the
Solution 1:
First, there is no need to add your executable icon as a data, putting it in icon
param would be enough.
Next, when you add a data to PyInstaller it would bring your data and extract it in temp
folder (e.g. C:\Users\Rat's Nest\Appdata\local\temp\_MEIXXXX\
) so you need to change your code to open your files from that directory. A good practice is to use this function in your code to retrieve your data. When running executable sys._MEIPASS
would be equal to the PyInstaller temp folder.
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
Then you can use it with something like new_source = resource_path("clogo.png")
.
Post a Comment for "How To Include Images In One File With Python Pyinstaller"