【问题标题】:Compiling Python into a windows application将 Python 编译到 Windows 应用程序中
【发布时间】:2015-02-02 20:04:05
【问题描述】:

目前我正在开发别人项目的一个分支,它是用 Python 编程语言编写的。

我可以访问我需要的所有源代码,以及我想要进行的所有更改,以及一切“设置”到我需要的方式。

我当前的步骤是尝试以某种方式编译它,因此它在独​​立应用程序中作为窗口运行。我知道这是可能的,因为这就是源应用程序的运行方式。目前,我可以使用 python 扩展模块、WinPython、构建 GUI 的 kivy 框架本身等访问 Visual Studio。

但我似乎无法弄清楚如何做到这一点。我粗略的研究建议了一个名为 py2exe 的程序,但据我所知,它不能满足我的需要。

【问题讨论】:

标签: python windows exe kivy


【解决方案1】:

复制自http://kivy.org/docs/guide/packaging-windows.html

创建规范文件

对于本示例,我们将打包 touchtracer 示例并嵌入自定义图标。 touchtracer 示例是 kivy\examples\demo\touchtracer 目录,主文件名为 main.py。

双击Kivy.bat,将打开一个控制台。

进入 pyinstaller 2.1 目录并创建初始规范:

cd pyinstaller-2.1
python pyinstaller.py --name touchtracer ..\kivy\examples\demo\touchtracer\main.py

您还可以将 icon.ico 文件添加到应用程序文件夹,以便为可执行文件创建图标。如果您没有可用的 .ico 文件,您可以使用 Web 应用程序 ConvertICO 将您的 icon.png 文件转换为 ico。将 icon.ico 保存在 touchtracer 目录中并输入:

python pyinstaller.py --name touchtracer --icon ..\kivy\examples\demo\touchtracer\icon.ico ..\kivy\examples\demo\touchtracer\main.py

有关更多选项,请参阅 PyInstaller 2 手册。

spec 文件将是 touchtracer.spec,位于 pyinstaller + touchtracer 目录中。现在我们需要编辑规范文件以添加 kivy 钩子以正确构建 exe。使用您喜欢的编辑器打开规范文件,并在规范的开头添加这些行:

from kivy.tools.packaging.pyinstaller_hooks import install_hooks
install_hooks(globals())

Analysis() 函数中,删除hookspath=None 参数。如果你不这样做,kivy 包钩子将根本不会被使用。

然后您需要更改COLLECT() 调用以添加touchtracer 的数据(touchtracer.kv、particle.png,...)。更改该行以添加 Tree() 对象。此树将搜索 touchtracer 目录中找到的每个文件并将其添加到您的最终包中:

coll = COLLECT( exe, Tree('../kivy/examples/demo/touchtracer/'),
               a.binaries,
               #...
               )

我们完成了。您的规范已准备好执行!

构建规范

双击Kivy.bat

转到 pyinstaller 目录,并构建规范:

cd pyinstaller-2.1
python pyinstaller.py touchtracer\touchtracer.spec

该包将位于 touchtracer\dist\touchtracer 目录中。

【讨论】:

  • 我刚才在 Kivy IRC 频道中了解到,由于我使用了 Christph Golke 的预编译 Windows 二进制文件,它没有附带特定于平台的 Kivy.bat。相反,我只需要正常启动一个命令外壳。
  • 好吧...我猜...通常 kivy 以 zip 形式提供,将 python 和其他所有东西捆绑在一起
猜你喜欢
  • 2016-01-21
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
相关资源
最近更新 更多