在 iPhone 应用程序中嵌入 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3691655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Embedding Python in an iPhone app
提问by Andrew Plotkin
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.
所以这是一个新的千年;苹果挥了挥手;现在在 iPhone (App Store) 应用程序中包含 Python 解释器是合法的。
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)
怎么做呢?所有现有的讨论(不出所料)都是指越狱。(较旧的问题:我可以使用 Python 编写本机 iPhone 应用程序吗)
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.
我的目标不是编写 PyObjC 应用程序,而是编写一个将 Python 作为嵌入式库运行的常规 ObjC 应用程序。然后 Python 代码将回调到本地 Cocoa 代码。这是“控制逻辑是 Python 代码”模式。
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.
是否有在 XCode 中构建 Python 的指南,以便我的 iPhone 应用程序可以链接它?最好是精简的 Python,因为我不需要 90% 的标准库。
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.
我大概可以弄清楚线程和 Python 扩展 API;我已经在 MacOS 上做到了。但只使用命令行编译器,而不是 XCode。
回答by bbum
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.
您如何构建 Python 并不重要——例如,您不需要在 Xcode 中构建它——但重要的是该构建的产品。
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.
也就是说,您将需要构建类似 libPython.a 之类的东西,它可以静态链接到您的应用程序中。一旦您有了 .a,就可以将其添加到您的应用程序的 Xcode 项目中,并且从那里开始,它将像应用程序的其余部分一样链接和签名。
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.
IIRC(自从我手动构建 python 已经有一段时间了)开箱即用的 python 将构建一个 libPython.a(和一堆其他库),如果你配置正确的话。
Of course, your second issue is going to be cross-compiling python for ARM from your 86box. Python is an autoconf based project and autoconf is a pain in the butt for cross-compilation.
当然,您的第二个问题将是从您的86 机器上为 ARM 交叉编译 python 。Python 是一个基于 autoconf 的项目,而 autoconf 是交叉编译的麻烦。
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.
毫不奇怪,您不是第一个想要这样做的人,但对于 iOS 而言并非如此。Python 已被挤入设备中,其功能远不如运行 iOS 的设备。我在谷歌搜索时发现了一个带有一堆链接的线程;它可能有用。
Also, you might want to join the pyobjc-devlist. 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.
此外,您可能想加入pyobjc-dev列表。虽然您的目标不是基于 PyObjC 的应用程序(顺便说一句,这是一个好主意 - PyObjC 在成为 iOS 友好型之前还有很长的路要走),但 PyObjC 社区一直在讨论这个问题,Ronald,任何人,可能是这个特定领域知识最渊博的人。请注意,在移植 PyObjC 之前,PyObjC 必须解决 iOS 上的嵌入式 Python 问题。他们的先决条件是您的要求,就像它一样。
回答by cobbal
I've put a very rough script up on github that fetches and builds python2.6.5 for iPhone and simulator.
我在 github 上放了一个非常粗略的脚本,它为 iPhone 和模拟器获取和构建 python2.6.5。
http://github.com/cobbal/python-for-iphone
http://github.com/cobbal/python-for-iphone
Work in progress
工作正在进行中
Somewhat depressing update nearly 2 years later: (copied from README on github)
近两年后有点郁闷的更新:(从github上的README复制)
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.
这个项目从来没有真正让 Python 在 iPhone 上运行让我满意,我不建议在现阶段将它用于任何严肃的项目。
最明显缺少的是 pyobjc 支持(事实证明它更难移植到 iPhone,因为它依赖于更多特定于平台的代码)
还缺少静态编译模块的能力,(目前所有模块都构建为 dylib,可用于开发,但据我所知在 App Store 中是不允许的)
在这一点上,这个项目主要是为了成为任何比我更聪明、想要并且能够解决上述问题的人的起点。
我真的希望完全用 Python 编写应用程序是可行的,但在这一点上似乎是不可能的。
回答by Albert
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.
我也开始了这样的项目。它带有自己的简化编译脚本,因此无需使用 autoconf 来获取交叉编译的静态库。它能够使用一些常用模块构建一个完全无依赖的 Python 静态库。它应该很容易扩展。

