PyQt to EXE

Summary: in this tutorial, you’ll learn how to convert a PyQt application to an executable program (EXE) on Windows

We’ll convert the Editor program to an executable file on Windows using PyInstaller. The following shows the directory structure of the Editor program:

├── assets
|  ├── editor.png
|  ├── exit.png
|  ├── new.png
|  ├── open.png
|  ├── redo.png
|  ├── save.png
|  └── undo.png
└── main.py
Code language: plaintext (plaintext)

In this project:

  • The assets directory stores all the images used by the program.
  • The main.py stores the program source code.

First, activate the virtual environment where the PyQt program runs.

Note that if you don’t use a virtual environment, PyInstaller may create an output with a big size that include all packages installed.

Second, use the following pip command to install PyInstaller:

pip install pyinstallerCode language: plaintext (plaintext)

PyInstall has a lot of options that are listed on this page. However, in this tutorial, we’ll show you the most commonly used ones.

Third, use the following pyinstaller command to convert the Editor program to an executable file:

pyinstaller main.py --noconsole --add-data "assets;assets" --icon="assets/editor.png" --name editor --noconfirmCode language: plaintext (plaintext)

Once you run this command successfully, you’ll see that PyInstaller creates new directories and files including build, dist, and editor.spec:

├── assets
|  ├── editor.png
|  ├── exit.png
|  ├── new.png
|  ├── open.png
|  ├── redo.png
|  ├── save.png
|  └── undo.png
├── build
|  └── editor
├── dist
|  └── editor
├── editor.spec
└── main.py

directory: 5 file: 9Code language: plaintext (plaintext)

Inside the dist/editor directory, you’ll find the editor.exe file. To launch the program, you can double-click it:

PyQt to EXE

How the pyinstaller command works:

  • --noconsole option hides the console window when the program launches.
  • --add-data "assets;assets" option copies the assets directory to the dist/editor directory so that the program can reference the images and display them correctly. The format of the --add-data option is SRC;DEST. If you want to copy multiple directories, you can use multiple --add-data options.
  • --icon="assets/editor.png" option specifies the icon for the main window.
  • --name editor option assigns a name to the program i.e., editor. If you ignore this option, PyInstaller will use the Python file as the program’s name e.g., main.
  • --noconfirm option will remove the existing build and dist directories without confirmation if you execute the command again.

Summary

  • Use Pyinstaller to convert a PyQt program to an executable file.
Did you find this tutorial helpful ?