1

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?

2
  • How exactly did you install over? Where is python 2.6.1 and 2.6.5? Commented Jun 18, 2010 at 11:27
  • I ment I installed 2.6.5 parallel to 2.6.1 but 2.6.5 is the primary python interpreter...most of the time it seems. I used the OS X installer disk image, never specified where the files should end up. 2.6.1 was preinstalled. I've done the same thing on two different mac's before (both snow leopard) without problem so I'm a little confused. Commented Jun 18, 2010 at 22:14

3 Answers 3

1

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 ... ." 
Sign up to request clarification or add additional context in comments.

4 Comments

This works, thanks! Just wish it would work without specifying interpreter, like this for example: do shell script "/Users/Me!/test.py"
You can put #! line at the beginning of the python file. See 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.
I have specified the correct shebang already :( The problem only occurs when applescript itself chooses python interpreter.
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.
0

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)'"

1 Comment

Both your solution works, I'll settle with one of them. Thanks!
0

The mac default install places python in /usr/bin, the python installer places it in frameworks with sym links in /usr/local/bin. Just remove the old version in /usr/bin and create new sym links

ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python /usr/bin/python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.