Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'],
)
like image 271
Serge Ballesta Avatar asked Oct 06 '20 14:10

Serge Ballesta


People also ask

Can you create a Mac app with Python?

py2app is a Python setuptools command which will allow you to make standalone Mac OS X application bundles and plugins from Python scripts. py2app is similar in purpose and design to py2exe for Windows. NOTE: py2app must be used on macOS to build applications, it cannot create Mac applications on other platforms.


1 Answers

Requirements

  • works with Python 3.8.5
  • macOS 10.15.7 Catalina
  • uses PySide2 and py2app

Problems

  • PySide2 must be added under OPTIONS to the packages list
  • when running the app then still an error occurs: Library not loaded: @rpath/libshiboken2.abi3.5.15.dylib, Reason: image not found

Solution

The slightly modified setup.py could look like this:

from setuptools import setup

APP = ['app.py']
DATA_FILES = []
OPTIONS = {
    'packages': ['PySide2'],
    'iconfile': 'some_icon.icns',
    'plist': {
        'CFBundleDevelopmentRegion': 'English',
        'CFBundleIdentifier': "com.ballesta.xxx",
        'CFBundleVersion': "1.0.0",
        'NSHumanReadableCopyright': u"Copyright © 2020, Serge Ballesta, All Rights Reserved"
    }
}

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

Additionally, an icon definition and a few plist entries for some basic information have been added.

The whole build is best triggered with a script that could look like this:

#!/bin/sh
python3 -m venv venv
. venv/bin/activate
pip install PySide2
pip install py2app
python setup.py py2app      
cp ./venv/lib/python3.8/site-packages/shiboken2/libshiboken2.abi3.5.15.dylib ./dist/app.app/Contents/Resources/lib/python3.8/lib-dynload/shiboken2

Test

Here the screenshot of a test run:

screenshot

like image 73
Stephan Schlecht Avatar answered Sep 28 '22 02:09

Stephan Schlecht