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 and to install it together with other components, like py2app, we will be using pip, the python package management system.

First we 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 link pip3 to pip-3.5 and python3 to python3.5.

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

# set python3 to python35
sudo port select --set python3 python35

Optionally you can also set the Python and pip version systemwide.

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

Thats's it!

Uco Mesdag

Read more posts by this author.

The Netherlands