Creating an executable (exe) from a PyQt GUI application can be a useful way to share your program with others who may not have Python or PyQt installed on their machines. In this tutorial, we will walk through the steps to create an exe file from a PyQt5 GUI application using PyInstaller.
Step 1: Install PyInstaller
The first step is to install PyInstaller, a Python package that can be used to convert Python scripts into standalone executables. You can install PyInstaller using pip by running the following command in your command prompt or terminal:
pip install pyinstaller
Step 2: Create your PyQt GUI application
Next, you will need to have your PyQt5 GUI application ready. If you don’t have one yet, you can create a simple PyQt5 GUI application to test the process. Here is an example of a simple PyQt5 application:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My PyQt App")
label = QLabel("Hello, PyQt!", self)
self.setCentralWidget(label)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Save this code to a file named my_app.py
.
Step 3: Create the exe file
Now that you have your PyQt5 GUI application ready, you can create the exe file using PyInstaller. Open your command prompt or terminal and navigate to the directory where your my_app.py
file is located. Run the following command to create the exe file:
pyinstaller --onefile my_app.py
This command will create a dist
directory in the same directory as your my_app.py
file. Inside the dist
directory, you will find an exe file that contains your PyQt5 application.
Step 4: Test the exe file
You can now test the exe file by running it from the command prompt or by double-clicking on it in your file explorer. If everything went well, you should see your PyQt5 application running as expected.
Step 5: Distribute the exe file
Once you have created the exe file for your PyQt5 application, you can distribute it to others who may want to use your program. You can share the exe file directly, or you can package it with an installer to make it easier for users to install the program on their machines.
In this tutorial, we walked through the steps to create an executable (exe) file from a PyQt5 GUI application using PyInstaller. By following these steps, you can easily share your PyQt5 applications with others without requiring them to have Python or PyQt installed on their machines.
Thanks so much for this. Finally got pyinstaller to create exe successfully. Have been struggling for so much time after following different videos on youtube.!!
Very good, thanks!!
Usually if an Internet user tries to compile his PyQt5 python scripts into an exe file he would like to find out how to make it without ordinary python methods and he hopes the PyQt5 developers have designed their own special way to do that simply.
By the way I don't know why you had to copy some dll files from the plugins directory. When I was "compiling" my scripts I didn't have to copy any files. That is the made exe file could work at once without any errors. Python version I used is 3.7 32bit.
Thanks a lot!
Solved the problem
Cross platform solution, no dependencies!!!
we killed mosquito at exact same time bruh 😂😂😂
Is there a way to bundle the platforms folder with the executable so that it is all in one executable file?
It is unbeliavable how things feel amateur in python.. I am used to program in C, C++, Java, even Visual Basic and only Python is full of those "tricks" to do such basic things!
i am getting this error…..RecursionError: maximum recursion depth exceeded….i am using python 3.7.4 and pyqt 5.9.2
I'm getting fatal error after building exe
hello ajay,
please create tutorial,
second_window back to first_window
What if main.py is already created like:
class class_App(QDialog):
def __init__(self):
super(class_App, self).__init__()
….
app = QApplication(sys.argv)
widget = class_App()
widget.show()
app = sys.exit(app.exec_())
….
And is calling for other Python file ? I could not figure it out how to do. I put all the py file in the dist/Main folder without success.
Thanks for your help
But why would it do this? Why does PyInstaller not have an option to make it work staight away after compiling? Why!???
Muchísimas gracias !!! me ayudaste un montón!!
You can make the cmd window automatically disappear while the application is running, using:
pyinstaller –onefile –windowed main.py
bahot hard bantaiii public.
Well done, sir!
but will it have problem if not find and installed msi-* files ?
Thanks for simple explanation.
Thank You! I also had this exact same error!
I freaking love you.