'Why Is pyinstaller replacing my file directory?

edit: How To Use --add-data with an entire folder?

Im converting my .py file to .exe using pyinstaller. but when i run it, it gives me this error:

Failed To Execute Script "test.py" due to unhandled exception: Couldn't open "C:\Users\Dani\AppData\Local\Temp_MEI11962\button_start.png" no such file or directory

Failed To Obtain/Convert Traceback!

Therefore My Converted Python File (exe) Was in the same directory As My Image. My path code is built like this:

from locate import this_dir
path = this_dir()
str(path) + "\\button_start.png"


Solution 1:[1]

A possible solution is to use pyinstaller's --add-data argument as shown in the documentation. You can either append this to your command when building the .exe or include that in a pyinstaller.spec file.

Additional information: When you execute a .exe, that was created with pyinstaller, it will create a temporary folder that is named _MEI<some numbers>, just as you showed above. In there, it expects all the necessary files. Therefor, this is also the place, where it will search for the image, no matter where you put your .exe to or where you execute it. Since this folder is generated temporarily every time you start the .exe, it would not help to simply copy your image there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fischmalte