I've written a program using the Pygame module and am trying to use it to create an .exe
which I can share with friends. In my Python scripts directory I have a folder which contains the .py
file and a folder called images
which contains the images used in the program. The program works just fine as a .py
, but as soon as I convert it to an .exe
via pyinstaller It fails to work.
Below is a minimum functional example:
import os
import pygame
def load_image(name):
fullname = os.path.join("images", name)
image = pygame.image.load(fullname)
return image
pygame.init()
screen = pygame.display.set_mode((500,500))
image = load_image("image.bmp")
clock = pygame.time.Clock()
while True:
screen.blit(image,(0,0))
pygame.display.flip()
clock.tick(60)
pygame.quit()
I compile this using pyinstaller --onefile example.py
.
I then execute from the command and receive the following error:
Traceback <most recent call last>:
File "<string>", line 11 in <module>
File "<string>", line 6 in load_image
pygame.error: Couldn't open images\image.bmp
example returned -1
I'm assuming this has something to do with local vs. global paths but no matter how much I fiddle I can't seem to get it up and running.
What do I need to change to be able to open the image file when using pyinstaller?