This page describes how you can compile an existing PyQt application to a self-contained .exe on Windows. You need Python 3.5 or 3.6. To see which version you have, simply start the Python interpreter:
If you see any value other than 3.5 or 3.6, it is highly recommended that you install Python 3.6.
You likely already have a Python environment with dependencies such as PyQt installed. We won't use it. Instead, we will create a new virtual environment. This guarantees that we have the correct library versions and that we don't interfere with your existing setup.
Open a command prompt in your project directory and execute the following:
%LOCALAPPDATA%\Programs\Python\Python36\python -m venv venv
This is if you installed 64-bit Python 3.6 in the default
location. You may have to update the path otherwise. Also
note that the portable / "embeddable" version that you can
download from python.org does not contain the required
venv module. You need a full installation.
Execute the command:
Your prompt should now have the prefix
The rest of this guide assumes that the virtual environment
is active, that is, that you always have the
venv prefix at the beginning of your prompt.
Type the following command:
pip install fbs PyQt5==5.9.2 PyInstaller==3.4
Your app may have other dependencies of its own. Use eg.
pip to install them as well.
You will likely have a
main.py script with code
similar to the following:
if __name__ == '__main__': app = QApplication(sys.argv) ... sys.exit(app.exec_())
Test that your app still works in the virtual environment by executing the following command:
For the sake of a more concrete example, we will assume for the remainder of this guide that your PyQt application consists of two files:
main.py has the following contents:
from PyQt5.QtGui import QPixmap from PyQt5.QtWidgets import QApplication, QLabel import sys if __name__ == '__main__': app = QApplication(sys.argv) window = QLabel() image = QPixmap('image.jpg') window.setPixmap(image) window.show() sys.exit(app.exec_())
You can use any picture you like for
It is shown in a window when you run the app:
Here's a visual diff of the changes (old version on the
left, new on the right):
In words: We take the contents of
if __name__ == '__main__' and put them inside
run() method of a new subclass of
ApplicationContext. We use
self.get_resource(...) to access
image.jpg. We no longer instantiate
QApplication(...). Instead, we access
self.app and return the result of its
exec_() function from our
method. In our new
if __name__ == '__main__'
block, we then instantiate our
pass the result of the
run() method to
We also need to add some files. This is best done via the following command:
You will be asked a few questions such as the name of your app. Feel free to use any values you like.
startproject command creates a
src/ folder in your current directory. Move our
main.py file from above into
src/main/python, overwriting the file that is
already there. Also move
image.jpg into the
Generally, if your app consists of more files than just the two above:
If all went well, you should now be able to use the following command to launch your app:
After the above steps, compiling your application to a stand-alone executable should be as easy as:
This creates the folder
target/MyApp in your
project directory, with the file
can copy the folder to any other computer to run your app
there. Isn't that awesome?
Creating an installer for your app is also just one command away:
This generates a typical Windows installer for your app:
Look through the other files in the
directory. There, you can customize things such as your
app's icon, or the main Python module.
If you encounter questions or run into any problems, please open an issue here.