简体   繁体   English

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

[英]How to create OS X app with Python on Windows

I need to automate a cross-platform application build.我需要自动化跨平台应用程序构建。 Entire build runs on Windows machine.整个构建在 Windows 机器上运行。 Part of it is written in Python and compiles for OS X. Currently this part of build is done manually on OS X.它的一部分是用 Python 编写的,并为 OS X 编译。目前这部分构建是在 OS X 上手动完成的。

I tried pyinstaller but it looks like it only building for the platform that it is running on.我试过 pyinstaller,但它看起来只为它运行的平台构建。 I also tried py2app but it did not install on Windows.我也试过 py2app 但它没有安装在 Windows 上。

Are there any tools to compile Python script to OS X app on Windows machine?是否有任何工具可以在 Windows 机器上将 Python 脚本编译为 OS X 应用程序?

Short answer:简短的回答:

Apparently, no simple way to do this with the standard set of tools you have mentioned.显然,使用您提到的标准工具集没有简单的方法可以做到这一点。 I outline a completely unprobable solution in the end that's probably too complex to consider.我最后概述了一个完全不可能的解决方案,它可能太复杂而无法考虑。

End result: Keep doing it manually, it's probably the best option so far.最终结果:继续手动操作,这可能是目前最好的选择。


Drawing from credible and/or official sources:取自可靠和/或官方来源:

There's already a long and curated list of options specified in micheal0x2a's excelent answer in Create a single executable from a Python project which outlines most tools available for creating standalone distributions of a Python program.从 Python 项目创建单个可执行文件micheal0x2a 的优秀答案中已经指定了一个很长且精心策划的选项列表,其中概述了大多数可用于创建 Python 程序的独立发行版的工具。 One line stands out:一行突出:

Also, unless otherwise noted, all programs listed below will produce an exe specifically for the operating system it's running in.此外,除非另有说明,否则下面列出的所有程序都将专门为其运行的操作系统生成一个 exe。

I might have read things wrong but there's no explicit note of cross platform support.我可能读错了,但没有明确说明跨平台支持。

Similar lists can be found in in Distribution Utilities and Freezing Your Code — The Hitchhiker's Guide to Python .类似的列表可以在Distribution UtilitiesFreezing Your Code — The Hitchhiker's Guide to Python 中找到 From these we can take a look at the Windows supported projects:从这些我们可以看看Windows支持的项目:


bbfreeze : Not maintained, no documentation on limitations generally superceded by the rest in this list. bbfreeze :未维护,没有关于限制的文档,通常被此列表中的其余部分取代。


PyInstaller : According to their documentation : PyInstaller :根据他们的文档

PyInstaller is tested against Windows, Mac OS X, and Linux. PyInstaller 已针对 Windows、Mac OS X 和 Linux 进行了测试。 However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows;但是,它不是交叉编译器:要制作 Windows 应用程序,您需要在 Windows 中运行 PyInstaller; 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.要制作 Linux 应用程序,请在 Linux 等中运行它。 PyInstaller 已成功用于 AIX、Solaris 和 FreeBSD,但未针对它们进行测试。

PyInstaller once tried to support cross-compilation (From Linux -> Windows) but later dropped the idea . PyInstaller曾经尝试支持交叉编译(从 Linux -> Windows)但后来放弃了这个想法


cx_freeze : Looking at their Freezing for other platforms question in their Frequently Asked Questions list: cx_freeze :在他们的常见问题列表中查看他们针对其他平台冻结问题

cx_Freeze works on Windows, Mac and Linux, but on each platform it only makes an executable that runs on that platform. cx_Freeze 适用于 Windows、Mac 和 Linux,但在每个平台上它只生成在该平台上运行的可执行文件。 So if you want to freeze your program for Windows, freeze it on Windows;因此,如果您想为 Windows 冻结程序,请在 Windows 上冻结它; if you want to run it on Macs, freeze it on a Mac.如果您想在 Mac 上运行它,请在 Mac 上冻结它。


py2app : This is not an option since py2app is supported only OSX machines based on their note: py2app :这不是一个选项,因为py2app仅基于他们的注释支持 OSX 机器:

NOTE: py2app must be used on OSX to build applications, it cannot create Mac applications on other platforms .注意:py2app 必须在 OSX 上使用来构建应用程序,它不能在其他平台上创建 Mac 应用程序

So installing it on windows is a no-go.所以在windows上安装它是不行的。

This wraps out the tools available on for creating standalone applications on Windows.这包含了可用于在 Windows 上创建独立应用程序的工具。 Even if not on Windows though, solutions don't exist for creating an OS agnostic tool .即使不在 Windows 上,也不存在用于创建与操作系统无关的工具的解决方案

The general consensus for achieving these sort of things is via Virtual Machines;实现这些事情的普遍共识是通过虚拟机; you create a VM image of the target OS, run the dedicated tool for that OS inside the vm and then tranfer the bundled executable to compatible machines.您创建目标操作系统的 VM 映像,在 vm 内运行该操作系统的专用工具,然后将捆绑的可执行文件传输到兼容机器。 Since OSX is generally not easy to virtualize, from what I know, you kinda run out of luck here.由于 OSX 通常不容易虚拟化,据我所知,你在这里有点走运了。


One complex probable way:一种复杂的可能方式:

Now, the reason why I said there is no simple way to do this is because there might be one but, from what I can see, it is so tedious you shouldn't even consider it.现在,我之所以说没有简单的方法可以做到这一点,是因为可能有一种方法,但是,据我所知,它太乏味了,您甚至不应该考虑。 The general issue we're facing here is that executables for windows are simply not compatible with OSX, heck, Linux executables aren't either .我们在这里面临的普遍问题是 Windows 的可执行文件与 OSX 根本不兼容,哎呀, Linux 可执行文件也不兼容。 So what we need is the ability to cross-compile things.所以我们需要的是交叉编译东西的能力。

One compiler that I've heard supports cross-compilation is clang .我听说支持交叉编译的一种编译器是clang So, in order remedy the incompatibility of executables, you could theoretically use clang as a cross compiler.因此,为了解决可执行文件的不兼容问题,理论上您可以使用 clang 作为交叉编译器。 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) .它可能比您愿意做的更难,因为它充满了复杂的问题(从 OSX 到 Windows 示例)

If you do actually find a way to that you now need a .cpp/.c file from your python scripts.如果您确实找到了一种方法,那么您现在需要一个来自 Python 脚本的.cpp/.c文件。 Thankfully this is something that can be done by using tools like Nuitka or Cython .幸运的是,这可以通过使用NuitkaCython等工具来完成。

The gist of these is the same, enter .py exit .cpp/.c .这些的要点是相同的,输入.py exit .cpp/.c The second tricky part might be setting up the clang compiler to be used with these.第二个棘手的部分可能是设置与这些一起使用的 clang 编译器。 I honestly have no idea if anyone has done this.老实说,我不知道是否有人这样做过。

I haven't looked into them much, but for Nuitka, I know that it can also be used to create a standalone executable when passed the --standalone flag (See: Moving to other machines).我没有对它们进行过多研究,但是对于 Nuitka,我知道它也可用于在传递--standalone标志时创建独立的可执行文件(请参阅:移至其他机器)。 So, theoretically you could use Nuitka with clang configured to cross-compile for OSX.所以,理论上你可以使用 Nuitka 和 clang 配置为 OSX 交叉编译。 Then you can rest;然后你就可以休息了; you've earned it.这是你应得的。

Install fabric (Python module, easily installed with pip) on your Windows machine so that you can run the build on your Mac as part of the same automated build process.在您的 Windows 机器上安装结构(Python 模块,可以通过 pip 轻松安装) ,以便您可以在 Mac 上运行构建作为同一自动化构建过程的一部分。

Fabric allows you to utilize SSH from Python. Fabric 允许您从 Python 中使用 SSH。 So long as you know how to access the Mac over SSH (just need the IP, username, and password), this is easy.只要您知道如何通过 SSH 访问 Mac(只需要 IP、用户名和密码),这很容易。 Set it up like this:像这样设置:

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>'

Then copy over the necessary source files from the Windows machine to the Mac like this (once fabric has been set up as above):然后像这样将必要的源文件从 Windows 机器复制到 Mac(一旦fabric按上述方式设置):

from fabric.operations import put

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

Now you want to run your build tool, whatever it is.现在你想运行你的构建工具,不管它是什么。 You'll need to use run or sudo for that, depending on if the tool requires admin privileges or not.您需要为此使用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')

Finally you have the build output which you can retrieve using get .最后,您可以使用get检索构建输出。

from fabric.operations import get

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

There we go.我们走了。 Pretty simple.很简单。 No VM needed - just automate the process that you were already manually doing before.不需要虚拟机 - 只需自动化您之前手动执行的过程。 The only real requirement is that your Mac has to be available to be connected to during the build process.唯一真正的要求是您的 Mac 必须在构建过程中可以连接。 If you don't have a readily available Mac server, consider just picking up a used Mac Mini - you can get them for as little as $50 on ebay, if budget is a concern.如果您没有现成的 Mac 服务器,可以考虑购买一台二手 Mac Mini - 如果预算有限,您可以在 ebay 上以低至 50 美元的价格购买。

You can use a docker image https://github.com/sickcodes/Docker-OSX like this to simulate a mac computer.您可以像这样使用 docker 镜像https://github.com/sickcodes/Docker-OSX来模拟 Mac 计算机。

Then from this simulated mac you could install the pyinstaller and run your command from there.然后从这个模拟的 mac 你可以安装 pyinstaller 并从那里运行你的命令。

This would then produce the desired file.这将产生所需的文件。

Some people do the same way to create windows executables using pyinstaller on linux.有些人在 linux 上使用 pyinstaller 以相同的方式创建 Windows 可执行文件。

I dont see why this could not work from windows to mac.我不明白为什么这不能从 windows 到 mac。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM