How to package a Python project into an exe with an uninstall function!

Mondo Technology Updated on 2024-01-30

Although I work on the web, I've always been interested in Python's many open source projects. The deployment documentation for source code such as the previous Stable Diffusion AI painting and SO-VITS-SVC tone replacement is complete. Despite this, it is not very friendly for students who are just getting started and who are building various pits in the local environment. In response to this phenomenon, there are already various bigwigs on the Internet who have made one-click deployment solutions, no need to install the python environment, no need to ** various dependency packages, and you can use it with a single click.

So how do they do it, by this point many people should already be talking about using pyinstaller. Yes, this does solve the overall environment packaging, but sometimes some icons and dependencies need to be put together to work properly, similar to the green desktop tool. Is there a way to further encapsulate this, that is, after clicking on the exe, it needs to be installed, and then after installation, it is the file of the project, and it can also be uninstalled, and the answer is to use the inno setup compiler. So, then I'll use a small demo of the GUI to complete the exe packaging and further installation and packaging.

python 3.8

tkinter (bring your own).

pyinstaller 6.3.0

inno setup compiler

Use conda to create a virtual environment separately, such as writing a tkinter application, by the way, in order to look good, we can set the following icons, the size is 32x32, and the large icon is 64x64 to prepare. Then use pyinstaller to achieve the application package (including exe), and finally use the inno setup compiler to achieve the installation package, which is the setting of various file associations, icons and information.

Environment creation

# 1.Create environment conda create -n tools env python=38 # 2.Enter the environment Conda Activate Tools env 3Install pyinstallerpip install pyinstaller -i
Coding examples

import tkinter as tkfrom tkinter import ttkfrom tkinter import messageboxfrom tooltip import tooltipimport osimport timeclass application(tk.tk): def __init__(self): super().init__(self.title("GZ number: Zero development") self.iconbitmap('logo.ico') self.geometry("600x370") to set a fixed window size selfresizable(false, false) disables resizing of window selfstyle = ttk.style(self) self.main_color = self.cget("bg") self.style.configure("tentry", padding=6, relief="flat", background="#0078d7", foreground="black", font=("arial", 12, "bold")) self.style.configure("tlabel", font=("arial", 12, "bold")) self.style.configure("tbutton", padding=6, font=("arial", 12)) self.create_widgets() def create_widgets(self): self.path_label = ttk.label(self, text='Absolute Path:') self.path_label.grid(row=0, sticky=tk.w, pady=30, padx=20) self.path = tk.strin**ar() self.path_entry = ttk.entry(self, width=60, textvariable=self.path) self.path_entry.grid(row=0, column=1, sticky=tk.e, pady=5) tooltip(self.path_entry, "The directory path in your computer, e.g. d: 3code 6pytorch pytorch ai demo") self.rename_label = ttk.label(self, text='Modify the name:') self.rename_label.grid(row=1, sticky=tk.w, pady=5, padx=20) self.rename = tk.strin**ar() self.rename_entry = ttk.entry(self, width=60, textvariable=self.rename) self.rename_entry.grid(row=1, column=1, sticky=tk.e, pady=5) self.msg_text = tk.text(self, height=2, width=60, wrap='none') to add a list box to display the file name selfmsg_text.grid(row=2, column=1, sticky=tk.w, pady=15) self.msg_text.configure(bd=0, relief="solid", bg=self.main_color) # self.msg_text.insert(tk.end, "This is the text to display. ") ttk.button(self, text='Confirm the changes', command=self.start_program).grid(row=3, column=1, sticky=tk.w, pady=20, padx=120) ttk.button(self, text='About the author', command=self.about).grid(row=4, column=1, sticky=tk.w, padx=120) def start_program(self): print("Absolute path: {} Renamed: {}". format(self.path.get(),self.rename.get())path = self.path.get() rename = self.rename.get() if path == "" or rename == "": messagebox.showwarning("Warning", "The input box cannot be empty!") return if os.path.isdir(path) == false: messagebox.showwarning("Warning", "The absolute path is incorrect!") return i = 0 # 'All files (including folders) in this folder' filelist = os.listdir(path) # 'Go through all the files' for files in filelist: olddirpath = os.path.join(path, files) self.msg_text.delete(1.0, tk.end) self.msg_text.insert(tk.end, olddirpath) # 'If it's a folder, it's called recursively' if os.path.isdir(olddirpath): self.start_program(olddirpath) # 'File name' filename = os.path.splitext(files)[0] # 'File extension' filetype = os.path.splitext(files)[1] filetype = filetype.lower() newdirpath = os.path.join(path, rename + "_" + str(i) +filetype) # 'Rename' os.rename(olddirpath, newdirpath) i += 1 messagebox.showinfo("information", "Operation done!") def about(self): messagebox.showinfo("About", "GZ No.: Zero Development Tools: Batch Modify File 10") def quit_program(self): self.destroy()if __name__ == "__main__": app = application() app.mainloop()
Start packing

pyinstaller -f -w application.py -–icon=logo.ico

Here we want to pack our own icon with icon, and don't show a black flash box with w when opening, of course, open the exe to put the icon in the same level of directory as him, the size has been said above, and the following is the parameter description.

Parameter description: -icon=iconpath-f Packaged into an exe file -w Use window, no console -c Use console, no window -d Create a directory that contains exe and other dependency files
Another problem is that if the ** is modified, it needs to be packaged many times, and you can delete the generated spec suffix file generated by the previous packaging of the project. After the packaging is successful, the control bread will display building exe from exe-00toc completed successfully, and then the dist of the root directory is the packaged package, and the files required in it** are all in it, and then use the inno setup compiler to package this folder.

inno setup compiler operation

Create a script and fill in the information.

Select the main program and the files you need.

Set the name and icon

Select the export location (there are also settings such as agreements in front).

Run the script to start the build.

The installation is opened, and there is a built-in uninstaller in the installation directory.

Related Pages