简体   繁体   中英

How to use pyinstaller to include images in the .exe

I am trying to make a simple pathfinding app with pygame and make it a.exe using pyinstaller. Unfortunatly, the project uses images, and when I run the.exe, it comes back with telling me the images dont exist. How do I run pyinstaller to include the images?

If you dont use the --onefile command, you can just drag and drop the images into the folder your exe is in. Or drop in the folder of images.

If you do use onefile, you need to modify the.spec file you get when you run the code pyinstaller script.py . Then run pyinstaller scriptname.spec . edit the datas variable somewhat like this

datas = [('src/image.png', '.'),
         ('src/image1.png' '.')]

If the images are in a different folder than your script you put the address of where they're at in regards to the script.

more information can be read in the documentation

Use cx-freeze instead when converting pygame to.exe. It works with images.

Link: https://cx-freeze.readthedocs.io/en/latest/setup_script.html

Follow instructions and when you build the.exe make sure you have the images you need in the same folder as the actual game itself.

Use this instead -

If this is the pixmap then add the function below and call it in pixmap

self.label.setPixmap(
            QtGui.QPixmap(
                self.resource_path("MyPNG_FILE.png")
            )
        )

Add this function to your code it will work for labels, images etc

### Use this function To attach files to the exe file (eg - png, txt, jpg etc) using pyinstaller
def resource_path(self, relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """

    if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM