Search code examples
python-3.xkivyexecutablepyinstallerkivy-language

Add image files while building kivy executable using pyinstaller


I wanted to create an ubuntu executable of my kivy project using pyinstaller. I have used kivy language in the project. With kivy language, I have added some images in the project. Now I wanted to make an executable. It makes executable and the executable also works fine with remaining project, except that images are not visible. I wanted to add the images which are in the resources folder. I don't really know how could I add these files in the project. I tries --add-data method, but that also doesn't work.


Solution

  • You do need to add the image with either the --add-data or with the datas element in your .spec file. Then you also need to make sure that your code can find the images. I use the following code when my images are in a resources folder:

    if getattr(sys, 'frozen', False):
        # this is a Pyinstaller bundle
        kivy.resources.resource_add_path(sys._MEIPASS)
        kivy.resources.resource_add_path(os.path.join(sys._MEIPASS, 'resources'))
    

    And then access the image files using:

    image_file_name = kivy.resources.resource_find('someImage.png')