Sunday, 11 October 2020

How to build an mac os app from a python script having a PySide2 GUI?

Context:

I am developping a simple Python application using a PySide2 GUI. It currently works fine in Windows, Linux and Mac. On Windows, I could use PyInstaller and InnoSetup to build a simple installer. Then I tried to do the same thing on Mac. It soon broke, because the system refused to start the command or the app generated by PyInstaller because it was not correctly signed. And as I am not an apple developper, I cannot sign anything...

After some research, I tried py2app. I can go one step further here. With

python setup.py py2app -A

I can create a runnable app. Which obviously cannot be ported to a different system because it uses my development folders. And if I use python setup.py py2app the generated program cannot start because py2app did not copy all the required Qt stuff. I tried to add one by one the missing libraries, but on the end the system could not find the plugins and I gave up...

Question:

Can someone help me with a recipe to convert a python script or package using a Qt GUI into a portable app on Mac? Ideally, the recipe should say how to use a custom application icon, but this is not required.

References:

  • Python 3.8.5
  • macOS 10.15.7 Catalina
  • PySide2 5.15.1
  • PyInstaller 4.0
  • py2app 0.22

As my real package is too large for a SO question I trimmed it down to a minimal reproducible example:

from PySide3.QtWidgets import *
import sys


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        hello = QLabel('Hello', self)
        hello.move(50, 50)

def run(args):
    app = QApplication(args)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    run(sys.argv)

And here is the setup.py file used for py2app:

from setuptools import setup

APP = ['app.py']
DATA_FILES = []
OPTIONS = {}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app": OPTIONS},
    setup_requires=['py2app'],
)


from How to build an mac os app from a python script having a PySide2 GUI?

No comments:

Post a Comment