Some system/software info before getting started:
OS: Mac OS X 10.7.1
Python: Active Python 2.7.2.5
wxPython: wxPython2.9-osx-2.9.1.1-cocoa-py2.7
I have a small wxpython-based Mac app that just tests the availability of cvs and svn on a Mac platform. This is the python code on which the Mac app is based:
import wx
import commands,os
ID_RUN_BUTTON=1
class Frame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(100, 100),style=wx.MINIMIZE_BOX | wx.CLOSE_BOX)
self.run_button=wx.Button(self,ID_RUN_BUTTON,label='Run')
self.Bind(wx.EVT_BUTTON, self.OnRun,id=ID_RUN_BUTTON)
self.Centre()
self.Show()
def OnRun(self,evt):
home_dir=os.path.expanduser("~")
a=commands.getoutput("cvs")
b=commands.getoutput("svn help")
f=open('%s/cvs_test' % (home_dir),'w')
f.write(a)
f.write('\n')
f.write(b)
f.close()
if __name__ == '__main__':
app = wx.App(redirect=False)
frame = Frame(None, -1, 'CVS Tester')
app.MainLoop()
Here is a screen shot of this simple GUI with one button called ‘Run’.

On pressing ‘Run’ it executes the OnRun method and it saves the output of the two commands ‘cvs’ and ‘svn help’ into a file called ‘cvs_test’ in the user’s home directory. When I run this code using the python interpreter on command-line, the output of both the commands is spit into a text file. Both cvs and svn commands are recognized and the output in the file cvs_test is as expected.
Now the problem is when I create a Mac app using py2app with the following script:
"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = ['cvs_test.py']
DATA_FILES = [('icons',['./icons/ark-2.png'])]
OPTIONS = {'iconfile': './icons/ark-2.icns'}
setup(app=APP,data_files=DATA_FILES,options={'py2app': OPTIONS},setup_requires=['py2app'])
The Mac app is created absolutely fine. But, when I open the Mac app and hit the ‘Run’ button, in the cvs_test file it created, it says:
“sh: cvs: command not found“
The Mac app is obviously running the same script but it couldn’t find the cvs command.
In my home directory, the following are the contents of my .profile file:
export TERM="xterm"
export PATH='/Developer/usr/bin':$PATH
export PATH='/usr/local/bin':$PATH
I added the path ‘/Developer/usr/bin’ by following a trick posted on Apple forums (click here) to resolve the cvs issue on OS X Lion.
What causes the python script to identify cvs when run from command-line and not identify it when the same script is run as a compiled Mac app ?
This question is killing me. My bash and the sh both of them can find cvs when run from the terminal, but the Mac app can’t. Any suggestions would be greatly appreciated.
When you run a OSX GUI app from Finder/Dock ie not the terminal a new shell is not started thus your .profile is not read.
You can set environment variables in ~/.MacOS/environment.plist as per Apple doc
You could also set the environment (e.g. add to the path) or explicitly use the full paths to cvs and svn in the python script