Before you start creating GUI applications with PyQt5, you need to have a working installation of PyQt5 on your system. In this step-by-step tutorial, you'll learn how to install PyQt5 on Ubuntu Linux using both pip and apt, so you can choose the method that best fits your project.
This guide is also available for macOS and Windows.
Note that the following instructions are for installing the PyQt5 version registered under the GPL license. If you need to use PyQt in a non-GPL project, then you will need to purchase an alternative license from Riverbank Computing to release your software.
Preparing Ubuntu to Install PyQt5
Before installing PyQt5, we need to prepare our operating system and install a couple of tools. Python best practices recommend using virtual environments to isolate our Python projects and manage their dependencies.
Unfortunately, the default Ubuntu installation doesn't include tools like the venv module, which allows us to create virtual environments, and the pip command, which lets us install external packages. So, we need to install them from the distro's repositories.
Installing venv and pip on Ubuntu
To install the venv module and the pip command in Ubuntu, run the following commands in your terminal:
$ sudo apt update
$ sudo apt install python3-venv python3-pip
The first command updates the package information from all sources of software we are using in our Ubuntu system. The second command downloads and installs the venv module and the pip command. Now, we are ready to create and activate a Python virtual environment.
Creating a Virtual Environment for PyQt5
Once we've installed the venv module, we can proceed to create and activate a Python virtual environment:
$ mkdir project/ && cd project/
$ python3 -m venv ./venv
$ source venv/bin/activate
(venv) $
First, we create a new working directory named project/ as a root for a hypothetical PyQt5 project. Then, we create a new virtual environment with venv and activate it. Note how the prompt indicator changes to signal that we're in an active virtual environment.
Install PyQt5 in a Virtual Environment With pip
Once we've created and activated our virtual environment, we can install PyQt5 with the following command:
(venv) $ pip3 install pyqt5
This command downloads PyQt5 from the Python package index (PyPI) and installs it in our virtual environment. Using pip is the recommended approach because it gives you access to the latest version of PyQt5 and keeps the installation isolated from your system Python.
Install PyQt5 System-Wide Using apt
Sometimes, we may need to install PyQt5 in the operating system rather than in a virtual environment. In Ubuntu's repositories, we'll find packages for PyQt5, although they may be out of date. Check first to ensure you're getting a version that meets your requirements, and if not, use the pip method above.
In Ubuntu, we can install either from the command line with the apt command or via the Software Center. The package we are looking for is python3-pyqt5.
To install PyQt5 from the command line, execute the following command:
$ sudo apt install python3-pyqt5
Verify the PyQt5 Installation
After the PyQt5 installation is finished, we can verify that everything is working correctly. Start a Python interactive session and import the library as shown below:
>>> import PyQt5
If the import completes without errors, PyQt5 is installed correctly. If you're using the pip installation, make sure you are in the terminal session with the active virtual environment. If you are using the apt installation, you can import the library from any terminal session.
Now that you've successfully installed PyQt5 on Ubuntu Linux, you can start creating Python GUI applications with PyQt5.
Packaging Python Applications with PyInstaller by Martin Fitzpatrick
This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.