Building a GUI application with PyQt on MacOS

A short how-to on building Python GUI applications on MacOS with PyQt5 and py2app.

Requirements

Install

PyQt5 depends on Python version 3.5 or greater and to install it together with other components, like py2app, we will be using pip, the python package management system.

First we have to install pip3 for Python version 3.5 with the use of MacPorts, this will automatically pull in Python 3.5 and resolve all its dependencies. Thereafter we will use pip3 to install PyQt5 and py2app.

# install pip3
sudo port install py35-pip

# install pyqt and py2app
sudo pip-3.5 install pyqt5 py2app

# create a symlink for py2applet
sudo ln -sf /opt/local/Library/Frameworks/Python.framework/Versions/3.5/bin/py2applet /opt/local/bin/py2applet

Building a MacOS application

Building a application bundle is straightforward. First you initialise your project, next you build it.

For development you can create your application bundle in alias mode (with the -A or --alias option), this uses your source and data files in-place. Note that in alias mode it does not create a standalone application, and the application built is not portable to other machines.

To build a standalone application you run it without the alias option.

# cd to your project directory
cd my project/

# create setup.py project file
py2applet --make-setup myapp.py

# build alias mode for development
python3.5 setup.py py2app -A

# clean between build
rm -rf build dist

# build release
python3.5 setup.py py2app

Optional

For convenience you can also set the Python and pip version systemwide with the following commands:

# set the python/pip version systemwide
sudo port select --set python python35
sudo port select --set pip pip35

Thats’s it!


© 2024. All rights reserved.