.exe file cannot find data files (No suche file or directory
I have created a game application sample using PyGame and tried to generate one file .exe via PyInstaller. I have generated .exe file that seems like fine, but it cannot run it because of the error below:
# error on my command pronpt when running main.exe
[Errno 2] No such file or directory: 'C:\\Users\\bggfr\\AppData\\Local\\Temp\\_MEI95~1\\item.data' IO Error ocarr.
The result of typing "pyi-archive_viewer" is like this and I can see "item.data" is properly combined with .exe file.
# pyi-archive_viewer main.exe
pos, length, uncompressed, iscompressed, type, name
this is my .spec file and I have made .exe file via this .spec file.
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pyz = PYZ(a.pure, a.zipped_data,
exe = EXE(pyz,
I'm sure that I need to use "_MEIPASS" when I read data files because the file path while running .exe file created by PyInstaller will be a temporary folder looking like above error(C:\\Users\\bggfr\\AppData\\Local\\Temp\\_MEI95~1).
I always use the function below where I need to read a data files(txt, img, font, etc...)
# a function that reads data files
def resource_path(self, directory, filename):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, filename)
return os.path.join(directory, filename)
Finally, I have used the latest version of PyInstaller explained in PyInstaller Web page