将Python嵌入iPhone应用程序

问题:将Python嵌入iPhone应用程序

因此,这是一个新的千年。苹果摇了摇手。现在在iPhone(App Store)应用程序中包含Python解释器是合法的。

如何做到这一点?现有的所有讨论(毫无疑问)都涉及越狱。(旧问题:我可以使用Python编写本机iPhone应用程序

我的目标不是编写PyObjC应用程序,而是编写将Python作为嵌入式库运行的常规ObjC应用程序。然后,Python代码将回调到本机可可代码。这是“控制逻辑就是Python代码”模式。

是否有使用XCode内置Python的指南,以便我的iPhone应用程序可以链接它?最好是精简的Python,因为我不需要90%的标准库。

我大概可以弄清楚线程和Python扩展API。我已经在MacOS上做到了。但仅使用命令行编译器,而不使用XCode。

So it’s a new millennium; Apple has waved their hand; it’s now legal to include a Python interpreter in an iPhone (App Store) app.

How does one go about doing this? All the existing discussion (unsurprisingly) refers to jailbreaking. (Older question: Can I write native iPhone apps using Python)

My goal here isn’t to write a PyObjC app, but to write a regular ObjC app that runs Python as an embedded library. The Python code will then call back to native Cocoa code. It’s the “control logic is Python code” pattern.

Is there a guide to getting Python built in XCode, so that my iPhone app can link it? Preferably a stripped-down Python, since I won’t need 90% of the standard library.

I can probably figure out the threading and Python-extension API; I’ve done that on MacOS. But only using command-line compilers, not XCode.


回答 0

如何构建Python其实并不重要-例如,您不需要在Xcode中构建它-但重要的是该构建的产品。

即,您将需要构建类似libPython.a的东西,该东西可以静态链接到您的应用程序中。有了.a后,可以将其添加到您的应用程序的Xcode项目中,然后从那里将其链接和签名,就像您应用程序的其余部分一样。

IIRC(自从我手工构建python以来已经有一段时间了),如果配置正确,现成的python将构建libPython.a(以及其他库)。

当然,您的第二个问题将是从86交叉编译适用于ARM的python盒。Python是一个基于autoconf的项目,而autoconf则是交叉编译的难题。

正如您正确指出的那样,减小尺寸至关重要。

同样,不足为奇的是您不是第一个想要这样做的人,但对于iOS而言却不是。Python已被挤入功能不如运行iOS的设备中。我在搜寻时发现了一个带有一堆链接的线程;这可能有用

另外,您可能想加入pyobjc-dev列表。虽然您的目标不是基于PyObjC的应用程序(顺便说一句,这是一个好主意-PyObjC在与iOS友好之前还有很长的路要走),但PyObjC社区一直在与Ronald讨论这个问题,在这个特定领域可能是最有知识的人。请注意,在移植PyObjC之前,PyObjC必须解决iOS上的嵌入式Python问题。他们的先决条件就是您的要求。

It doesn’t really matter how you build Python — you don’t need to build it in Xcode, for example — but what does matter is the product of that build.

Namely, you are going to need to build something like libPython.a that can be statically linked into your application. Once you have a .a, that can be added to the Xcode project for your application(s) and, from there, it’ll be linked and signed just like the rest of your app.

IIRC (it has been a while since I’ve built python by hand) the out-of-the-box python will build a libPython.a (and a bunch of other libraries), if you configure it correctly.

Of course, your second issue is going to be cross-compiling python for ARM from your 86 box. Python is an autoconf based project and autoconf is a pain in the butt for cross-compilation.

As you correctly state, making it small will be critical.

Not surprising, either, is that you aren’t the first person to want to do this, but not for iOS. Python has been squeezed into devices much less capable than those that run iOS. I found a thread with a bunch of links when googling about; it might be useful.

Also, you might want to join the pyobjc-dev list. While you aren’t targeting a PyObjC based application (which, btw, is a good idea — PyObjC has a long way to go before it’ll be iOS friendly), the PyObjC community has been discussing this and Ronald, of anyone, is probably the most knowledgeable person in this particular area. Note that PyObjC will have to solve the embedded Python on iOS problem prior to porting PyObjC. Their prerequisite is your requirement, as it were.


回答 1

我在github上放了一个非常粗糙的脚本,该脚本为iPhone和模拟器获取并构建python2.6.5。

http://github.com/cobbal/python-for-iphone

工作正在进行中

大约2年后的更新令人沮丧:(从README复制在github上)

这个项目从来没有真正让python在iPhone上运行过,我不满意,因此我不建议在此阶段将它用于任何严肃的项目。

最值得注意的是缺少pyobjc支持(事实证明,由于它依赖于更多平台特定的代码,因此很难移植到iPhone)

还缺少静态编译模块的能力(目前所有模块都以dylib形式构建,可用于开发,但据我所知App Store不允许这样做)

在这一点上,这个项目的主要目的是成为比我更聪明并且想要解决上述问题的任何人的起点。

我真的希望完全用Python编写应用程序是可行的,但目前看来这是不可能的。

I’ve put a very rough script up on github that fetches and builds python2.6.5 for iPhone and simulator.

http://github.com/cobbal/python-for-iphone

Work in progress

Somewhat depressing update nearly 2 years later: (copied from README on github)

This project never really got python running on the iPhone to my satisfaction, and I can’t recommend using it for any serious project at this stage.

Most notably missing is pyobjc support (which turns out to be much harder to port to iPhone since it relies on more platform-specific code)

Also missing is the ability to statically compile modules, (all are currently built as dylibs which works for development, but to my knowledge wouldn’t be allowed in the App Store)

At this point this project is mostly meant to be a starting point for anyone smarter than me who wants to and can tackle the above issues.

I really wish it were practical to write apps entirely in Python, but at this point it seems impossible.


回答 2

我也开始了这样的项目。它带有自己的简化编译脚本,因此无需弄乱autoconf即可获得交叉编译的静态库。它能够使用一些通用模块构建完全无依赖的Python静态库。它应该易于扩展。

https://github.com/albertz/python-embedded/

I also started such a project. It comes with its own simplified compile script so there is no need to mess around with autoconf to get your cross compiled static library. It is able to build a completely dependency-free static library of Python with some common modules. It should be easily extensible.

https://github.com/albertz/python-embedded/