This guide assumes you already have an python code that can running locally, and it has a GUI built by Pyside6 or PyQt6. How to create an App so you can share with your friends or the public, even they don’t install python on their computer? The most common method is using the tool py2app
.
from setuptools import setup
APP = ['main.py']
DATA_FILES = []
OPTIONS = {
'argv_emulation': True,
'packages': ['PySide6'],
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Replace 'main.py'
with the name of your main script. If your application uses additional data files (like images or text files), you should list them in DATA_FILES
.
python setup.py py2app -A
The above is the whole process. I wish it could be that simple. However, in practice, if there is no accident, there will definitely be an accident. Here are some tips.
Launch Error
Sometimes the app in the file folder dist
can’t open: it says Launch Error. But you don’t know why.
Here is a tip: don’t open it by double click the app, but use command in terminal. It will be much easier if you know the error message. It costs me nearly one whole night to find out this tip
Although it looks that there is only one file demo.app
in the dist
file, but acutally not. You can use the following code to run the app:
./dist/demo.app/Contents/MacOS/demo
If the App doesn’t run, the error message will tell you why.
In my case, it says that
libshiboken6.abi3.6.6.dylib’ (no such file)
What the hell is this file? Never heard of. Anyway, I used two ways to solve this problem:
1, in the setup.py
file, I added the path of this file to the framworks
.
from setuptools import setup
APP = ['main.py']
DATA_FILES = []
OPTIONS = {
'packages': ['PySide6'],
'frameworks': ['/System/Volumes/Data/Users/henrywu/MyDrive/99_Coding/01-Github/test/venv/lib/python3.11/site-packages/shiboken6/libshiboken6.abi3.6.6.dylib']
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
2, Since in the error message, it say no such file and try to find it from many path. I thought if I copy the file to one of these path, for example, “/usr/local/lib/libshiboken6.abi3.6.6.dylib’ (no such file)”, it might solve the problem. It turns out it works!
Debug by Simply Start
You can also create a simple “Hello World” application using PySide6. This process helps determine if the problem is specific to your application or a more general issue with your py2app setup.
import sys
from PySide6.QtWidgets import QApplication, QLabel
def main():
app = QApplication(sys.argv)
label = QLabel('Hello World!')
label.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()
python hello_world.py
from setuptools import setup
APP = ['hello_world.py']
DATA_FILES = []
OPTIONS = {
'argv_emulation': True,
'packages': ['PySide6'],
}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
python setup.py py2app
./dist/hello_world.app/Contents/MacOS/hello_world
Debug by Reset Everything
Sometimes, it is difficult to tell what’s the problem. So one solution is … restart your computer. Just kidding. But reset the environment always a good start to repair something.
You can delete the existing venv file and rebuild it by requirments.txt
.
First, test if you can run your code locally. If you can, then generate a requirments.txt
file by the following code:
pip freeze > requirements.txt
Delete your old environment:
deactivate
rm -r /path/to/your/venv
Create a new virtual environment:
python3 -m venv your/new/venv
Activate the new virtual environment:
source /path/to/your/new/venv/bin/activate
Install necessary packages:
pip install -r requirements.txt
Note: in the terminal, python3
and python
is different, while pip3
and pip
is also different. If one is not working, you can consider change to another one.