/ Software Development

Create Python Executable for Windows Using Pyinstaller

Ashiqur Rahman

Read more posts by this author.

Read More
Create Python Executable for Windows Using Pyinstaller

Today we will write a small piece of application with Python using PyQt5 and also make executable(.exe) to run it on windows pc.

The Code

Now let's see the code below:

#!/usr/bin/env python

from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout, QPushButton, QStyleFactory)

class WidgetGallery(QDialog):
    def __init__(self, parent=None):
        super(WidgetGallery, self).__init__(parent)
        self.setMinimumSize(400, 200)

        self.numOfClick = 0;
        self.btn = QPushButton("Click Me!!!!!")

        mainLayout = QGridLayout()
        mainLayout.addWidget(self.btn, 0, 0, 1, 1)

        self.setWindowTitle("Click Example")

    def clickAction(self):
        self.numOfClick += 1
        self.btn.setText("Clicked " + str(self.numOfClick) + " times");

if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)
    gallery = WidgetGallery()

The above application is very simple. We have opened a dialog box. We have set a title and a layout in that dialog box. In the layout we have added a button with a click action. When clicked it updates the button text with how may times it has been clicked. This is our plain and simple application.

First when app launched
After some click actions


Now we want to ship our application to our users. So need we need one executable file(.exe) to give them. Their are various libraries to do it. We will use pyinstaller package to make the executable.

So first install pyinstaller using following command in terminal:

pip install pyinstaller

Or if your are using conda then you can also install it using following command in proper environment:

conda install -c conda-forge pyinstaller

Now If we run the following command our executable will be generated after pyinstaller had finished it work. You will find it as <our_python_script_name>.exe file.

pyinstaller --onefile <our_python_script_name>.py

See, it is that simple!!!!!

If you have any question regarding this, ask below. Happy coding :) .....