Create Exe from Python script with images with Pyinstaller

What is pyinstaller

The pyinstaller module makes you able to create a standalone exe file for pc from a python script, so that you can use it on other computers than do not have python or your python version.

To install pyinstaller:

pip install pyinstaller

Create an exe

After you installed pyinstaller, go in the folder where your python script to be transformed in exe is and type in the command line (go in the address bar on top of the window and type cmd):

pyinstaller yourscript.py

where your script is the name of your file.

You will find a folder called “dist” where there is a folder called yourscript (if the name of your script was this) where there are many files and the exe file that will allow you to run the script also on other computer. You just have to copy the whole folder.

What if there are images that the script uses?

If you use images, that are in the same folder, for example, you can copy manually into the folder made by pyinstaller or you can run this command to make it do it for you:

pyinstaller –add-data “*.png;.” yourscript.py

This way you will see that in the folder where your exe is there are also the png files (all of them, if you want just one do not put the asterisc, but the whole name of the file.

The video about making an exe with images from a python script

My contribute to stackoverflow

The code that you see in the video to browse images

I am gonna improve this script soon. It shows in a canvas the images in the folder.

import tkinter as tk
import glob


def insertfiles():
    for filename in glob.glob("*.png"):
        lst.insert(tk.END, filename)


def showimg(event):
    n = lst.curselection()
    filename = lst.get(n)
    img = tk.PhotoImage(file=filename)
    w, h = img.width(), img.height()
    print(filename)
    canvas.image = img
    canvas.config(width=w, height=h)
    canvas.create_image(0, 0, image=img, anchor=tk.NW)


root = tk.Tk()

root.geometry("800x600+300+50")
lst = tk.Listbox(root, width=20)
lst.pack(side="left", fill=tk.BOTH, expand=0)
lst.bind("<<ListboxSelect>>", showimg)
insertfiles()
canvas = tk.Canvas(root)
canvas.pack()

root.mainloop()

 


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Auto-py-to-exe to include images into the exe

Read this new post about this topic and see how to use auto-py-to-exe to make everything easier. Pay attention to the video because in order to use one exe file with images included you need to add something to the scripts that uses images to make the path to the images relative and referred to the exe file.

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.