py2app

要使用 py2app 框架,必須先安裝它。通過開啟終端並輸入以下命令來執行此操作:

sudo easy_install -U py2app

你也可以通過以下方式安裝軟體包:

pip install py2app 

然後為 python 指令碼建立安裝檔案:

py2applet --make-setup MyApplication.py

根據自己的喜好編輯設定檔案的設定,這是預設設定:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup

APP = ['test.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

要新增圖示檔案(此檔案必須具有 .icns 副檔名),或在應用程式中包含影象作為參考,請更改你的選項,如下所示:

DATA_FILES = ['myInsertedImage.jpg']
OPTIONS = {'argv_emulation': True, 'iconfile': 'myCoolIcon.icns'}

最後輸入到終端:

python setup.py py2app

該指令碼應該執行,你將在 dist 資料夾中找到已完成的應用程式。

使用以下選項進行更多自定義:

optimize (-O)         optimization level: -O1 for "python -O", -O2 for
                      "python -OO", and -O0 to disable [default: -O0]

includes (-i)         comma-separated list of modules to include

packages (-p)         comma-separated list of packages to include

extension             Bundle extension [default:.app for app, .plugin for
                      plugin]

extra-scripts         comma-separated list of additional scripts to include
                      in an application or plugin.