[英]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.我创建了一个 wxPython GUI 应用程序,我想分发它以在 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. MyApp.py
文件运行script.py
并且script.py
引用resources
文件夹中的文件。
To create the .app
bundle, I've used Pyinstaller
like so:为了创建
.app
包,我使用Pyinstaller
,如下所示:
cd /path/to/root/folder
pyinstaller MyApp.py --windowed
This creates an .app
file but this immediately closes when I try to open it.这会创建一个
.app
文件,但是当我尝试打开它时它会立即关闭。
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'
FileNotFoundError:[Errno 2] 没有这样的文件或目录:'/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.在
script.py
中,我使用os.getcwd()
获得了对root
目录的引用,但是在尝试访问resources
文件夹时,运行应用程序似乎没有使用此相对路径。 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:然后,例如,您的
file1.json
冷访问使用:
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
命令行(或.spec
文件)中指定它们,您的resources
文件夹中的两个文件可能不会包含在您的应用程序文件夹中:
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:您可以只在
--add-data
选项中指定文件夹,如下所示:
pyinstaller --windowed --add-data "resources:resources" MyApp.py
But I have never tried exactly that.但我从未尝试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.