If you wonder how to make an .exe file to distribute your python app, just read this post and you’ll see how to do it with auto-py-to exe.
Brent Vollebregt is the author of this great app.
Pyinstaller
First you need to install pyinstaller, as the program is based on this module.
pip install pyinstaller
Auto-py-to-exe
Now you can install auto-py-to-exe.
pip install auto-py-to-exe
Run auto-py-to-exe
Now you can run it like so:
I cannot include images in the exe
One problem could be this: you have to put the images into the folder of the exe file. But why you cannot include the images into the exe file instead, so that i have one only file to distribute with my app?
Here is the solution.
Once you set the Auto-py-to-exe like this, if you have an like like the following
Video
The code of the example in the video
import tkinter as tk import os from tkinter import messagebox import sys ''' Try one of this 3 functions and see which one works ''' def resource_path0(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ base_path = getattr( sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))) return os.path.join(base_path, relative_path) def resource_path(relative): return os.path.join( os.environ.get( "_MEIPASS2", os.path.abspath(".") ), relative ) return os.path.join(base_path, relative_path) def resource_path2(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) def works(evt): messagebox.showinfo("Image in the exe file", "This really works") root = tk.Tk() # Here is where you use resource_path for your images to fix the path so that works with pyinstaller # to include the images in the exe file path = resource_path0("image2.png") img = tk.PhotoImage(file=path) label = tk.Label(root, image=img) label.pack() label.bind("<1>", works) root.mainloop() """ pyinstaller --noconfirm --onefile --windowed --add-data "G:/filebrowser/image2.PNG;." "G:/filebrowser/app2.py" """
Remember to put an image called “image2.png” in the folder of this script to make it work. If resource_path0 does not work, try resorce_path or resource_path2.
Another example of use of the functions resource_path0 in another game
If you liked this post, come here more often to find other interesting curiosity about python.
Older video about this
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame