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.