views:

3503

answers:

5

I'm trying to run Python scripts using Xcode's User Scripts menu.

The issue I'm having is that my usual os.sys.path (taken from ~/.profile) does not seem to be imported when running scripts from XCode the way it is when running them at the Terminal (or with IPython). All I get is the default path, which means I can't do things like

#!/usr/bin/python
import myScript

myScript.foo()

Where myScript is a module in a folder I've added to my path.

I can append a specific path to os.sys.path manually easily enough, but I have to do it in every single script for every single path I want to use modules from

Is there a way to set this up so it uses the same path I use everywhere else?

EDIT: After looking into things a bit more, it seems like scripts executed from Xcode use a completely different PATH than normal. The path I get by running a script in Xcode is:

PATH=/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin

and I'm sure my regular path doesn't have /Developer/usr/bin in it. Does anybody have any idea where this path is coming from?

+1  A: 

A quick but hackish way is to have a wrapper script for python.

cat > $HOME/bin/mypython << EOF
#!/usr/bin/python
import os
os.path = ['/list/of/paths/you/want']
EOF

and then start all your XCode scripts with

#!/Users/you/bin/mypython
Toby White
Thanks, I'll try that.
Lawrence Johnston
+1  A: 

Just add the paths to sys,path.

>>> import sys
>>> sys.path
['', ... lots of stuff deleted....]
>>> for i in sys.path:
...     print i
... 

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
>>> sys.path.append("/Users/crm/lib")
>>> for i in sys.path:
...     print i
... 

/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload
/Library/Python/2.5/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC
/Users/crm/lib
>>>
Charlie Martin
Well, that's what I've been doing, but I don't know of any way to persist my changes beyond that single script (since setting PYTHONPATH has no effect in this situation). Are you aware of something I'm missing?
Lawrence Johnston
A: 

Forgive me if my answer seems ignorant, I'm not totally familiar with Mac and I also may have misunderstood your question.

On Windows and Linux, when I want to refer to a script I've written, I set the PYTHONPATH environment variable. It is what os.sys.path gets its values from, if I remember correctly.

Let's say myScript.py is in /Somewhere. Set PYTHONPATH to:

PYTHONPATH = /Somewhere

Now you should be able to "import myScript".

If you start doing sub-folders as python packages, look into usage of init.py files in each folder.

If you plan on re-using this and other scripts all the time, you should leave PYTHONPATH set as an environment variable.

Chris Cameron
That's just it, the normal place to set PYTHONPATH mac side is in ~/.profile, which I've done. This works properly when using the python or IPython interpreters or running an executable python script from the finder. It's only when trying to run them from inside Xcode I run into problems.
Lawrence Johnston
When I develop Python applications in PyDev in Eclipse, PyDev removes things that aren't in site-packages from PYTHONPATH. So to reference those scripts I have to tell it what "projects" I am referencing, or in this case, the path to your scripts. Perhaps XCode has this setting/feature as well?
Chris Cameron
+4  A: 

On the mac, environment variables in your .profile aren't visible to applications outside of the terminal.

If you want an environment variable (like PATH, PYTHONPATH, etc) to be available to xcode apps, you should add it to a new plist file that you create at ~/.MacOSX/environment.plist.

See the EnvironmentVars doc on the apple developer website for more details.

Ted Naleid
+1  A: 

I tend to use pth files. From the docs.

The most convenient way is to add a path configuration file to a directory that’s already on Python’s path, usually to the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path. (Because the new paths are appended to sys.path, modules in the added directories will not override standard modules. This means you can’t use this mechanism for installing fixed versions of standard modules.)

So the simplest thing to do is to do the following:

echo "/some/path/I/want/to/add" > /Library/Python/2.5/site-packages/custom.pth

HTH

rh0dium