[Pythonmac-SIG] Launching Python Subprocesses with Py2App

Bob Ippolito bob at redivi.com
Fri Apr 29 15:57:50 CEST 2005


On Apr 29, 2005, at 9:37 AM, Bob Swerdlow wrote:

> I am upgrading our application from bundlebuilder to py2app.  It  
> looks great and it looks like it will run on Tiger, which my  
> bundlebuilder version does not.

Something to finally put the nails in the coffin..

> However, I've run into a problem because our application launches  
> subprocesses to run other Python scripts.  In the old version with  
> bundlebuilder, sys.executable was in the same location as the  
> Resources, so we used that path to find the file containing the  
> main script for the new process and then used subprocess.POpen to  
> launch sys.executable with that path as an argument.  This does not  
> work with py2App because sys.executable is no longer in the same  
> place as the other files and the modules are compressed into site- 
> packages.zip.
>
> I found the FAQ on how to locate the bundle resources, but the  
> files that I want python to run cannot be accessed from the command  
> line (since it is in site-packages.zip).  I tried adding a copy of  
> the main script for one of the new process as a 'resource', and I  
> can launch it, but site-packages.zip is not in its PYTHONPATH and  
> so it cannot find the other modules it needs.
>
> So, the question is, how do I configure my code using py2app so  
> that I can launch a subprocess that runs one of the python scripts  
> embedded in the bundle?

This use case is not currently well supported by py2app.  If you're  
using py2app from svn, sys.executable will point to something, but  
PYTHONPATH won't quite be setup properly.

If you do this somewhere before you start spawning subprocesses, it  
might work:

# written in Mail...
import os
import sys
# this is definitely currently necessary
os.environ['PYTHONPATH'] = ':'.join([p for p in sys.path if isinstance 
(p, basestring)])
# don't know if this is necessary, might still be set from the py2app  
bootstrap
os.environ['PYTHONHOME'] = sys.prefix

But again, I don't support this very well yet because none of my  
applications need it and I have a bunch of higher priority things to  
deal with, so YMMV.  Patches accepted.

Some future version of py2app is going to move sys.executable into  
the MacOS folder, so don't start hard coding any weird hacks based  
upon where it happens to sit.

-bob



More information about the Pythonmac-SIG mailing list