如何在 Windows 上使用 Python 创建 OS X 应用程序

How to create OS X app with Python on Windows

我需要自动构建跨平台应用程序。整个构建在 Windows 机器上运行。它的一部分是用 Python 编写的,并为 OS X 编译。目前这部分构建是在 OS X 上手动完成的。

我试过 pyinstaller,但看起来它只针对 运行 所在的平台构建。我也试过 py2app 但它没有安装在 Windows.

是否有任何工具可以将 Python 脚本编译到 Windows 机器上的 OS X 应用程序?

简答:

显然,使用您提到的标准工具集没有简单的方法可以做到这一点。我最后概述了一个完全不可能的解决方案,它可能太复杂而无法考虑。

最终结果:继续手动操作,这可能是迄今为止最好的选择。


来自可靠的 and/or 官方来源:

micheal0x2a's excelent answer in Create a single executable from a Python project 中已经指定了一个长而精选的选项列表,其中概述了可用于创建 Python 程序的独立分发版的大多数工具。一行突出:

Also, unless otherwise noted, all programs listed below will produce an exe specifically for the operating system it's running in.

我可能看错了,但没有明确说明跨平台支持。

可以在 Distribution Utilities and Freezing Your Code — The Hitchhiker's Guide to Python 中找到类似的列表。从这些我们可以看看 Windows 支持的项目:


bbfreeze:未维护,没有关于限制的文档通常被此列表中的其他内容取代。


PyInstaller: According to their documentation:

PyInstaller is tested against Windows, Mac OS X, and Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a Linux app you run it in Linux, etc. PyInstaller has been used successfully with AIX, Solaris, and FreeBSD, but is not tested against them.

PyInstaller曾经试图支持cross-compilation(从Linux -> Windows)但是later dropped the idea .


cx_freeze: Looking at their Freezing for other platforms question in their Frequently Asked Questions 列表:

cx_Freeze works on Windows, Mac and Linux, but on each platform it only makes an executable that runs on that platform. So if you want to freeze your program for Windows, freeze it on Windows; if you want to run it on Macs, freeze it on a Mac.


py2app:这不是一个选项,因为 py2app 仅支持 OSX 机器,根据他们的注释:

NOTE: py2app must be used on OSX to build applications, it cannot create Mac applications on other platforms.

所以在 windows 上安装它是一个 no-go。

这包含了可用于在 Windows 上创建独立应用程序的工具。即使不在 Windows 上, 也不存在创建 OS 不可知工具的解决方案

实现这些事情的普遍共识是通过虚拟机;您创建目标 OS 的 VM 映像,运行 是 vm 中 OS 的专用工具,然后将捆绑的可执行文件传输到兼容机器。由于 OSX 通常不容易虚拟化,据我所知,你有点 运行 运气不好。


一种复杂的可能方式:

现在,我之所以说没有简单的方法可以做到这一点,是因为可能有一个,但据我所知,它太乏味了,你甚至不应该考虑它. 我们在这里面临的一般问题是 windows 的可执行文件根本不兼容 OSX,哎呀,Linux executables aren't either所以我们需要的是cross-compile事物的能力。

我听说过的一个编译器supports cross-compilation is clang. So, in order remedy the incompatibility of executables, you could theoretically use clang as a cross compiler. Problem is, this is not a simple task; it is probably way harder than what you're willing to do since it is riddled with complex issues (From OSX to Windows example).

如果您确实找到了方法,您现在需要 python 脚本中的 .cpp/.c 文件。值得庆幸的是,这可以通过使用像 Nuitka or Cython 这样的工具来完成。

这些大意是一样的,进入.py退出.cpp/.c。第二个棘手的部分可能是设置 clang 编译器以与这些一起使用。老实说,我不知道是否有人这样做过。

我没有深入研究它们,但对于 Nuitka,我知道它也可以用来创建 standalone executable when passed the --standalone flag(参见:移动到其他机)。因此,理论上 您可以使用 Nuitka,将 clang 配置为 cross-compile for OSX。然后你就可以休息了; 你赚到了。

在您的 Windows 机器上安装 fabric (Python module, easily installed with pip),这样您就可以 运行 在您的 Mac 上进行构建,作为同一自动构建过程的一部分。

Fabric 允许您从 Python 使用 SSH。只要您知道如何通过 SSH 访问 Mac(只需要 IP、用户名和密码),这很容易。像这样设置:

from fabric.api import env

env.host_string = '<IP of the Mac Here>'
env.user        = '<Username on the Mac>'
env.password    = '<Password of the user>'

然后像这样从 Windows 机器复制必要的源文件到 Mac(一旦 fabric 如上设置):

from fabric.operations import put

put(r'\path\to\local\source\files', '/path/to/where/you/want/them')

现在您想要 运行 您的构建工具,无论它是什么。您需要为此使用 runsudo,具体取决于该工具是否需要管理员权限。

from fabric.operations import run, sudo

# sudo works the same as run if you need that instead
run('/path/to/build/tool arguments for build tool')

您终于有了可以使用 get 检索的构建输出。

from fabric.operations import get

get('/path/to/dist/on/mac', '\local\path\to\store\on\windows')

好了。很简单。不需要 VM - 只需自动化您之前已经手动执行的过程。唯一真正的要求是您的 Mac 必须可以在构建过程中连接到。如果您没有随时可用的 Mac 服务器,请考虑购买二手 Mac Mini - 如果预算有限,您可以在 ebay 上以低至 50 美元的价格购买。

您可以像这样使用 docker 图像 https://github.com/sickcodes/Docker-OSX 来模拟 mac 计算机。

然后从这个模拟 mac 您可以安装 pyinstaller 和 运行 您的命令。

这将生成所需的文件。

有些人使用相同的方法在 linux 上使用 pyinstaller 创建 windows 可执行文件。

我不明白为什么这在 windows 到 mac 之间不起作用。