目录
为macOS创建软件包¶
备注
本指南介绍了打包Kivy应用程序的多种方法。对于一般用途,推荐使用Kivy SDK进行打包。
使用Kivy SDK¶
备注
这些说明仅适用于Kivy v2.0.0及更高版本。
备注
Kivy.app 是使用 MACOSX_DEPLOYMENT_TARGET=10.9 构建的。
我们提供了一个Kivy DMG,其中所有依赖项都打包在一个**虚拟环境**中,包括一个可作为打包Kivy应用基础的Python解释器。
这是最安全的方法,因为它打包的二进制文件不引用打包应用所在系统上的任何二进制文件。所有引用都指向dmg中包含的框架或dmg内的二进制文件。这与例如pyinstaller不同,后者会从你本地Python安装中复制二进制文件。
你可以在 kivy-sdk-packager 仓库 的 readme 中找到使用 Kivy.app 构建和打包应用的完整指南,无论是从 Kivy.app 开始还是从头构建。
使用 Buildozer¶
pip install git+http://github.com/kivy/buildozer cd /to/where/I/Want/to/package buildozer init
编辑buildozer.spec文件,并添加你的应用详情。依赖项可以添加到`requirements=`部分。
默认情况下,requirements 中指定的 kivy 版本会被忽略。
如果你在 /Applications/Kivy.app 路径下有一个 Kivy.app,那么打包时会使用它。否则,将下载并使用来自 kivy.org 的最新构建版本(基于 Kivy master 分支)。
当你准备好打包你的 macOS 应用时,只需运行::
buildozer osx debug
应用打包完成后,您可能希望移除不必要的包,仅将包缩减至应用运行所需的最小状态。
就这样。尽情享受吧!
Buildozer 目前使用 Kivy SDK 来打包您的应用。如果您希望比 Buildozer 当前提供的功能更精细地控制应用的细节,您可以直接使用 SDK,具体方法详见下文。
使用PyInstaller和Homebrew¶
备注
请在你希望支持的最老 macOS 版本上打包你的应用。
完整指南¶
安装 Homebrew
安装Python:
$ brew install python
备注
要使用Python 3,请执行``brew install python3``,并在下面的指南中将``pip``替换为``pip3``。
使用
--build-from-source重新安装你的依赖项,以确保它们可以在其他机器上使用:$ brew reinstall --build-from-source sdl3 sdl3_image sdl3_ttf sdl3_mixer
备注
如果你的项目依赖于GStreamer或其他附加库,请按照`下方 <additional libraries>`_所述,使用``--build-from-source``(重新)安装它们。
安装Cython和Kivy:
$ pip install Cython==3.2.0 $ pip install -U kivy
安装PyInstaller:
$ pip install -U pyinstaller
使用您的main.py路径打包您的应用程序:
$ pyinstaller -y --clean --windowed --name touchtracer \ --exclude-module _tkinter \ --exclude-module Tkinter \ --exclude-module enchant \ --exclude-module twisted \ /usr/local/share/kivy-examples/demo/touchtracer/main.py
备注
这还不会复制额外的图像或声音文件。你需要为此调整生成的
.spec文件。
编辑spec文件¶
规格文件名为 touchtracer.spec,位于你运行 pyinstaller 命令的目录中。
你需要修改 COLLECT() 调用,以添加 touchtracer 的数据(touchtracer.kv、particle.png 等)。将该行修改为添加一个 Tree() 对象。这个 Tree 会搜索并添加 touchtracer 目录中找到的每个文件到你的最终包中。你的 COLLECT 部分应该看起来像这样:
coll = COLLECT(exe, Tree('/usr/local/share/kivy-examples/demo/touchtracer/'),
a.binaries,
a.zipfiles,
a.datas,
strip=None,
upx=True,
name='touchtracer')
这将添加必要的钩子,以便PyInstaller获取所需的Kivy文件。我们已完成。您的spec文件已准备好执行。
构建规范并创建DMG文件。¶
打开控制台。
前往PyInstaller目录,并构建spec文件:
$ pyinstaller -y --clean --windowed touchtracer.spec
运行:
$ pushd dist $ hdiutil create ./Touchtracer.dmg -srcfolder touchtracer.app -ov $ popd
现在,dist 目录中将有一个 Touchtracer.dmg 文件可用。
附加库¶
GStreamer¶
如果你的项目依赖于GStreamer:
$ brew reinstall --build-from-source gstreamer gst-plugins-{base,good,bad,ugly}
备注
如果你的项目需要Ogg Vorbis支持,请确保在上述命令中添加``--with-libvorbis``选项。
如果你正在使用Homebrew安装的Python,那么在`这个拉取请求 <https://github.com/Homebrew/homebrew/pull/46097>`_ 被合并之前,你还需要执行以下步骤:
$ brew reinstall --with-python --build-from-source https://github.com/cbenhagen/homebrew/raw/patch-3/Library/Formula/gst-python.rb
使用PyInstaller而不使用Homebrew¶
首先,按照此处所述,在不使用Homebrew的情况下安装Kivy及其依赖项:http://kivy.org/docs/installation/installation.html#development-version。
安装好Kivy及其依赖后,您需要安装PyInstaller。
假设我们使用一个名为 testpackaging 的文件夹:
cd testpackaging
git clone https://github.com/pyinstaller/pyinstaller
在此目录中创建一个名为touchtracer.spec的文件,并向其中添加以下代码:
# -*- mode: python -*-
block_cipher = None
from kivy.tools.packaging.pyinstaller_hooks import get_deps_all, hookspath, runtime_hooks
a = Analysis(['/path/to/yout/folder/containing/examples/demo/touchtracer/main.py'],
pathex=['/path/to/yout/folder/containing/testpackaging'],
binaries=None,
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
hookspath=hookspath(),
runtime_hooks=runtime_hooks(),
**get_deps_all())
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='touchtracer',
debug=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe, Tree('../kivy/examples/demo/touchtracer/'),
Tree('/Library/Frameworks/SDL3_ttf.framework/Versions/A/Frameworks/FreeType.framework'),
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='touchtracer')
app = BUNDLE(coll,
name='touchtracer.app',
icon=None,
bundle_identifier=None)
将路径替换为您的相关路径:
a = Analysis(['/path/to/yout/folder/containing/examples/demo/touchtracer/main.py'],
pathex=['/path/to/yout/folder/containing/testpackaging'],
...
...
coll = COLLECT(exe, Tree('../kivy/examples/demo/touchtracer/'),
然后运行以下命令:
pyinstaller/pyinstaller.py touchtracer.spec
在适当的地方将 touchtracer 替换为你的应用名称。这将在 dist/ 文件夹中生成一个 <yourapp>.app 文件。