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

标签 python macos build pyinstaller py2app

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

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

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

最佳答案

简答:

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

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


来自可靠和/或官方来源:

micheal0x2a's 中已经指定了一长串精选的选项 中的优秀答案 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 中找到 Freezing Your Code — The Hitchhiker's Guide to Python 。从这些我们可以看一下 Windows 支持的项目:


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


PyInstaller :根据他们的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 曾经尝试支持交叉编译(从 Linux -> Windows)但是 later dropped the idea .


cx_freeze :查看他们的 Freezing for other platforms 在他们的 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 上安装它。

这包装了可用于在 Windows 上创建独立应用程序的工具。即使不在 Windows 上,也不存在创建与操作系统无关的工具的解决方案

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


一种复杂的可能方式:

现在,我之所以说没有简单的方法可以做到这一点,是因为可能有一个,但据我所知,它太乏味了,你甚至不应该考虑它。 我们在这里面临的一般问题是 Windows 的可执行文件与 OSX 根本不兼容,见鬼,Linux executables aren't either . 所以我们需要的是交叉编译东西的能力。

我听说过的一个编译器 supports cross-compilation clang 。因此,为了弥补可执行文件的不兼容性,理论上您可以使用 clang 作为交叉编译器。问题是,这不是一项简单的任务;自从 it is riddled with complex issues (From OSX to Windows example) 以来,这可能比您愿意做的要难得多.

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

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

我没有深入研究它们,但是对于 Nuitka,我知道它也可以用来创建一个 standalone executable when passed the --standalone flag (请参阅:移动到其他机器)。因此,理论上您可以将 Nuitka 与配置为针对 OSX 交叉编译的 clang 一起使用。然后你就可以休息了; 这是你应得的。

关于python - 如何在 Windows 上使用 Python 创建 OS X 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772216/

相关文章:

python - 如何在 Windows 中运行扭曲试用版?

python - 如何使用pycrypto将RSA加密数据存储到postgresql?

python - PIL 中的 PNG 显示在 OS X Mavericks 上损坏了吗?

java - 如何从 SBT Play Framework 2.3 项目中的源中排除目录

xcode - 防止 Xcode 在任何文件更改时重建项目

jenkins - 如何更改 Jenkins 保留的构建数量?

python - QComboBox 项目文本在 Windows 上被截断

python - 根据标签位置计算 Pandas DataFrame 的索引

python - 在 Python 中使用 ctypes.util.find_library 获取库的完整路径

objective-c - Osascript - 无输出