在 iPhone 应用程序中嵌入 Python

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

一个人如何做到这一点? 所有现有的讨论(不出所料)都涉及越狱。(老问题: 我可以使用 Python 编写本地 iPhone 应用程序吗)

我在这里的目标不是编写一个 PyObject 应用程序,而是编写一个常规的将 Python 作为嵌入式库运行的 OBC 应用程序。然后 Python 代码将调用本机 Cocoa 代码。这是“控制逻辑是 Python 代码”模式。

有没有使用 XCode 构建 Python 的指南,这样我的 iPhone 应用程序就可以将它链接起来?最好是一个简化的 Python,因为我不需要90% 的标准库。

我可能能够搞清楚线程和 Python 扩展 API; 我在 MacOS 上已经做过了。但只能使用命令行编译器,而不能使用 XCode。

50168 次浏览

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.

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.

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/