0

I'm trying to compile a stand alone exe with PyInstaller following the instructions here on SO, but I'm unable to include my two .png image files in the process.

I have added this code to my one and only script, main.py

def resource_path(relative_path):
        """ Get absolute path to resource, works for dev and for PyInstaller """
        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.path.abspath(".")

        return os.path.join(base_path, relative_path)

And I have defined my images background.png and closed.png properly

background = resource_path("background.png")
close = resource_path("closed.png")

I create the .spec file and have added this line to it

a.datas += [('icon.ico', 'C:\Users\Michael\Desktop\lolping\icon.ico','DATA'), ('background.gif','C:\Users\Michael\Desktop\background.png','DATA'), ('closed.png','C:\Users\Michael\Desktop\closed.png','DATA')],

But after building my .exe and running it I get this error:

File "main.spec", line 12
    a.datas += [('background.png', 'C:\\Deskop\\background.png','DATA',
    ('closed.png', 'C:\\Desktop\\closed.png','DATA')],
                                  ^
SyntaxError: invalid syntax
Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

-1

Have you tried to use double backslash \ instead of just using one in

a.datas+= [('icon.ico', 'C:\Users\Michael\Desktop\lolping\icon.ico','DATA'), ('background.gif','C:\Users\Michael\Desktop\background.png','DATA'), ('closed.png','C:\Users\Michael\Desktop\closed.png','DATA')], 

I believe that you might be required to write two in order for python to understand that your typing backslash. In the link below double backslash is used, so I believe the answer to your problem lies in not using double backslash but only one. I.e write

 a.datas+= [('icon.ico', 'C:\\Users\\Michael\\Desktop\\lolping\\icon.ico','DATA'), ('background.gif','C:\\Users\\Michael\\Desktop\\background.png','DATA'), ('closed.png','C:\\Users\\Michael\\Desktop\\closed.png','DATA')], 

Add image to .spec file in Pyinstaller

Community
  • 1
  • 1
Nautilius
  • 1,581
  • 2
  • 12
  • 14