views:

49

answers:

2

I recently installed python 2.6.5 over 2.6.1 on OS X. Everything works fine except when I try executing python scripts via the Applescript editor. If I execute the following line I get version 2.6.1:

Applescript:

do shell script "python -c \"from sys import version; print version\""

But if I execute the similar line in the OS X terminal i get version 2.6.5:

OS X terminal:

python -c "from sys import version; print version"

What could be wrong? Can this somehow be easily fixed?

+1  A: 

I won't recommend overwriting system-installed python (or whatever UNIX commands,) because some of the system app might depend on the quirk of a particular version of python installed.

It's quite customary these days to install another distinct copy of python (or perl or ruby) in an entirely different path. Then you set up the path appropriately in .profile etc. in the case of bash so that in the command line python you installed will be chosen.

But note that the GUI app inherits the environment variables from the system and don't read .profile or whatever. There's a way to change PATH environment variable seen by the GUI apps (see here), but I don't recommend it.

Where did you install your python 2.6.5? Say it's in

/usr/local/bin/python

Then in your AppleScript code you can just say

do shell script "/usr/local/bin/python  -c ... ." 
Yuji
This works, thanks! Just wish it would work without specifying interpreter, like this for example: do shell script "/Users/Me!/test.py"
bennedich
You can put `#!` line at the beginning of the `python` file. See http://en.wikipedia.org/wiki/Shebang_(Unix) . You need to write something like `#!/usr/local/bin/python` at the head. And yes, you need to put the full path there.
Yuji
I have specified the correct shebang already :( The problem only occurs when applescript itself chooses python interpreter.
bennedich
That's weird. If you have the correct shebang, and the file has the executable bit, do shell script "/path/to/script.py" should just work, invoking the interpreter specified in the shebang.
Yuji
A: 

The Python 2.6.5 installer modifies your ~/.bash_profile file to put Python 2.6.5's bin directory at the start of your $PATH, ahead of /usr/bin which is where the OS's default Python interpreter lies. That profile is automatically read when you log in via Terminal, but not when using do shell script. You either need to specify the full path to the python executable you want (probably the best option), or you could have your script read the bash profile beforehand (though be aware that do shell script uses sh, not bash):

do shell script ". ${HOME}/.bash_profile; python -c 'import sys; print(sys.version)'"
has
Both your solution works, I'll settle with one of them. Thanks!
bennedich