Luke Prior
Luke Prior

Reputation: 967

Bundling Python with pyinstaller --onefile --add-data not working

I used pyinstaller --add-data "icon.png;." --add-data "casino.png;." debug2.pywhich works fine but when I make a single file using pyinstaller --onefile --add-data "icon.png;." --add-data "casino.png;." debug2.py the executable no longer works.

I believe this is an issue with relative paths maybe?

Here is my python code for loading these assets:

icon = pygame.image.load('icon.png')
image_path="casino.png",

What can I do to get this working?

Upvotes: 0

Views: 383

Answers (1)

Luke Prior
Luke Prior

Reputation: 967

The code was fine on the pyinstaller end but when using --onefile the assets are unpacked in a temporary file so the python code was looking in the wrong directory.

To fix it I had to add

try:
   wd = sys._MEIPASS
except AttributeError:
   wd = os.getcwd()

icon_path = os.path.join(wd,"icon.png")

casino_path = os.path.join(wd,"casino.png")

Then change my paths for icon & casino to:

icon = pygame.image.load(icon_path)
image_path=casino_path,

Upvotes: 1

Related Questions