(TL;DR: Create a python .plugin bundle with
py2app, load it in
@objcprotocols to specify concrete interfaces for the python classes to implement. Example here.)
(This is an update to an earlier article for compatability with Swift 3 and XCode 8.1. There has been a couple of name and interface changes, but nothing major.)
I wanted to use an existing library of python code that I had written, in a new macOS application - to provide an easy-to-use UI for the library. I wanted to use Swift, both because I find it an infinitely nicer language than Objective-C, and it seems Swift is now accepted as mainstream.
There is lots of information around on how to integrate python with Objective-C via the
pyobjcpython library, but a lot of information is very old and I couldn’t find anything that discussed or was even as recent as Swift.
The best way to create executable bundles with python is with
py2app, which has two modes of operation - creating an executable application
.appbundle with the main executable written in python and calling into compiled swift code, and creating a python-based
.pluginbundle that is loaded by the Swift-based application. After having various issues working with the
.appmethod, I decided to use the
.pluginapproach, despite the relative lack of documentation.
This article shows a very basic application to demonstrate the fundamental principals of integrating swift and python.[Read More →]
Update 2017/11/4: I’ve updated this post for compatability with Swift 3 and XCode 8.1 here.[Read More →]