97

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.

Community
  • 1
  • 1
Andrew Plotkin
  • 1,176
  • 1
  • 8
  • 9

3 Answers3

30

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Thanks for the links. (I used Python on Sharp Zaurus for a *much* earlier version of this project, but that was Python 1.6 and somebody else did the packaging...) – Andrew Plotkin Sep 11 '10 at 18:02
  • 4
    I once ported Python to Sony's PSP; maybe some of the Subversion history will help you. Not exactly an answer though. here: http://www.python-psp.net/trac/ – fraca7 Nov 14 '10 at 14:19
22

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.

cobbal
  • 69,903
  • 20
  • 143
  • 156
  • Thanks! I won't have a chance to look at it for a few days, but I'll definitely try it soon. – Andrew Plotkin Sep 14 '10 at 16:46
  • If `pyobjc` is difficult to compile, is it feasible to use `ctypes`? In that case [this](http://stackoverflow.com/a/1490644/288672) answer might be a clue (realising it'd just be the start of a large and painful work, but still). – Jacob Oscarson Jul 06 '12 at 10:12
10

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/

Albert
  • 65,406
  • 61
  • 242
  • 386
  • I don't get your compilescript to work. It does compile, but lacks of some builtin modules like '-collections'. Any ideas? – bijan Oct 26 '12 at 10:40
  • I didn't included all the native C modules. Just modify it. – Albert Oct 26 '12 at 13:11
  • Seems dated... only works with iOS 4.3? Could you document how to get this to work with more current versions? – ArtOfWarfare Aug 16 '15 at 14:39
  • @ArtOfWarfare: Haven't used it in a while, by why shouldn't it work with more recent versions? Maybe make a bug report on GitHub if you see any problems. – Albert Aug 16 '15 at 14:48