Deploying PyQt 5 on Windows: A Guide from Installation to Exe Deployment using PyInstaller

Posted by


PyQt5 is a popular Python library that allows you to create desktop applications with a graphical user interface. In this tutorial, we will walk you through the installation of PyQt5 on Windows, creating a simple PyQt5 application, and packaging it into an executable using PyInstaller.

  1. Installation of PyQt5 on Windows:

The first step in using PyQt5 is to install it on your Windows machine. You can install PyQt5 using pip, the Python package manager. Open a command prompt and run the following command:

pip install PyQt5

This will install the latest version of PyQt5 on your machine. Once the installation is complete, you can verify that PyQt5 has been installed correctly by importing it in a Python script:

import PyQt5

If the import statement does not throw any errors, then PyQt5 has been successfully installed on your machine.

  1. Creating a simple PyQt5 application:

Next, let’s create a simple PyQt5 application to test our installation. Create a new Python script and add the following code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle('PyQt5 Tutorial')
window.setGeometry(100, 100, 400, 200)

button = QPushButton('Click me', window)
button.setGeometry(150, 70, 100, 30)

window.show()

sys.exit(app.exec_())

This code creates a simple PyQt5 window with a button that says "Click me". Save the script as simple_app.py and run it in the command prompt:

python simple_app.py

You should see a window pop up with a button. Click the button to make sure the application is running as expected.

  1. Packaging the PyQt5 application into an executable using PyInstaller:

Now that we have created a simple PyQt5 application, let’s package it into an executable that can be run on any Windows machine without having to install Python or PyQt5. We will use PyInstaller, a tool that freezes Python applications into standalone executables.

First, install PyInstaller using pip:

pip install pyinstaller

Next, navigate to the directory containing your simple_app.py script and run the following command to package your application into an executable:

pyinstaller --onefile simple_app.py

PyInstaller will create a dist folder containing the executable file. Navigate to the dist folder and run the generated executable:

cd dist
simple_app.exe

You should see the same window with the button you created earlier. This executable can be distributed to other Windows users without requiring them to install Python or PyQt5.

In this tutorial, we have covered the installation of PyQt5 on Windows, creating a simple PyQt5 application, and packaging it into an executable using PyInstaller. PyQt5 is a powerful library for creating desktop applications with a graphical user interface, and PyInstaller makes it easy to distribute your applications to other users. I hope this tutorial has been helpful in getting you started with PyQt5 on Windows. Happy coding!

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x