简体   繁体   中英

Pyinstaller .app bundle won't open on Mac

I've created a wxPython GUI application and I want to distribute it to be run on macOS.

First of all, here's my folder structure:

root/
├── MyApp.py
|
├── scripts/
|   ├── script.py
|
├── resources/
|   ├── file1.json
|   ├── file2.txt

The MyApp.py file runs script.py and script.py references the files in the resources folder.

To create the .app bundle, I've used Pyinstaller like so:

cd /path/to/root/folder
pyinstaller MyApp.py --windowed

This creates an .app file but this immediately closes when I try to open it.

To investigate the issue, I went here:

MyApp.app > Contents > MacOS > MyApp (A Unix executable)

This runs the app from the Terminal, but I get this error:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/MyAccount/resources'

In script.py , I get a reference to the root directory using os.getcwd() , but running the app seems not to use this relative path when trying to access the resources folder. I'm very new to this so I'm not sure where I might be going wrong, any help would be greatly appreciated!

You can use the runtime information to get the path to the folder where your code is located:

import sys
import os

if getattr(sys, 'frozen', False):
    # this is a Pyinstaller bundle
    root_path = sys._MEIPASS
else:
    # normal python process
    root_path = os.getcwd()

Then, for example, your file1.json cold be accessed using:

path_to_file1 = os.path.join(root_path, 'resources', 'file1.json')

However, the two files in your resources folder will probably not get included in your app folder without specifying them in your Pyinstaller command line (or in a .spec file):

pyinstaller --windowed --add-data "resources/file1.json:resources" --add-data "resources/file2.txt:resources" MyApp.py

You might be able to just specify the folder in the --add-data option, like this:

pyinstaller --windowed --add-data "resources:resources" MyApp.py

But I have never tried exactly that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM