使用py2app创建一个独立的OSX应用

安装py2app

1
pip install py2app

在要打包的文件夹内创建setup.py文件

hex.py 是要打包的工程文件

1
2
3
4
py2applet --make-setup hex.py
如果成功会显示
Wrote setup.py

setup.py文件是对应用的基本定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from setuptools import setup
APP = ['hex.py']
APP_NAME = "Hex"
DATA_FILES = []
OPTIONS = {
'argv_emulation': True,
'iconfile': 'app.icns',
'plist': {
'CFBundleName': APP_NAME,
'CFBundleDisplayName': APP_NAME,
'CFBundleGetInfoString': "otto.hex",
'CFBundleIdentifier': "com.otto.osx.hex",
'CFBundleVersion': "1.0.0",
'CFBundleShortVersionString': "1.0.0",
'NSHumanReadableCopyright': "Copyright @ 2017, otto.Wu, All Rights Reserved"
}
}
setup(
name=APP_NAME,
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)

1.应用图标及其他资源文件都与setup.py在同一目录下
2.如果你的应用使用的其他资源文件,应该将其包含在DATA_FILES中。

1
DATA_FILES = ['testdata.json', 'picture.png']

发布应用(如果提示没有权限,记得加sudo)

1
2
3
4
//删除本地缓存
rm -rf build dist
//打包
python setup.py py2app