1

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?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
CoffeeCrow
  • 123
  • 1
  • 4
  • Have you tried sending the whole path? – Hugo sama Jul 15 '17 at 15:07
  • Possible duplicate of [Pyinstaller and --onefile: How to include an image in the exe file](https://stackoverflow.com/questions/31836104/pyinstaller-and-onefile-how-to-include-an-image-in-the-exe-file) – Maurice Meyer Jul 15 '17 at 15:14

1 Answers1

2

You need to tell pyinstaller about the data files with something like:

pyinstaller --add-data 'images/image.bmp:.' --onefile example.py

From the (DOCS)

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135