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...
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.
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'],
)
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.
Requirements
Problems
PySide2
must be added under OPTIONS to the packages listLibrary 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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With