PyQt5 to exe file

In this post you’ll get to know about, how you can convert your PyQt5 GUI program(s) into a standalone .exe file or executable for any platform. You just need to install pyinstaller in your Python.

pip install pyinstaller
python3 -m pip install pyinstaller

The first and the primary requirement is to have a Python code, that has a GUI part. I may be as simple as a single Qlabel to as complex as monitoring some applications’ performance UI. But the basic process remains the same.

Here I have created a very basic UI using PyQt5 and designed the layout using QtDesigner.
The UI just asks you to enter a name and it displays some “Hello” message under the LineEdit widget as follows:

Now if you want to distribute this UI among other people and you want that they cannot modify your code. So you have to create an application. Fortunately, this is pretty easy in Python.

Though many options are available for converting Python GUI to exe, I’ll prefer using pyinstaller as it is platform-independent and very easy to add resource files(if you have any to the stand-alone application file).

Converting .py file to .exe file in Python using pyinstaller

The task can be completed using a single-line command.

pyinstaller -F


pyinstaller --onefile

N.B.: -F is the shortcut to the –onefile. Here, I have used for setting an example. You can replace that by your script’s name.

One thing you should take care of. You should open the terminal from the same folder as your GUI script. After successfully executing the command above, you can see two folders generated. One is dist and build. Your required .exe file or the executable will be available inside the dist directory. Another file will also be generated with the name python_file.spec according to example. This spec file is important if your program has some other resources dependencies.

Leave a Reply

Your email address will not be published.