Skip to content

How to build a standalone MacOS X application

astrofrog edited this page Jun 30, 2012 · 3 revisions

In theory, there are several options to building a standalone MacOS X application, but so far the most promising is py2app (http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html). The standard procedure is to create the 'master' script which is what gets executed when the application is launched, and to do:

py2applet --make-setup
python setup.py py2app

If setup.py only contains pure python, everything works fine, but if Matplotlib or PyQt4 are imported, the following error is raised:

ValueError: New Mach-O header is too large to relocate

It looks like the solution is to re-install MacPorts, and change one of the configuration files (http://www.danplanet.com/blog/2009/02/15/using-py2app-with-gtk/). Specifically, after installing MacPorts, one needs to edit /opt/local/share/macports/Tcl/port1.0/portconfigure.tcl and to set the line starting with:

default configure.ldflags   ...

to:

default configure.ldflags   {"-L${prefix}/lib -Xlinker -headerpad_max_install_names"}

After this, packages should be re-installed from source:

sudo port install -s PyQt4

(which will take a while). I have not tested this yet because ideally we should create a virtual machine to do this, but I will try and re-build everything on my laptop over the next few days.

There is useful information here too: http://aralbalkan.com/1675

While it'll be useful to have a .app file, we may also want to be able to run things from the command line, so we need to investigate whether we can launch the python interpreter inside the .app file and have access to all the installed modules. If so, then this can also serve as a self-contained Python distribution!

I've started to compile simple examples here: https://github.com/astrofrog/py2app-experiments - to compile, just run:

python setup.py py2app

and the executable will be placed inside dist/.