简体   繁体   中英

Applescript uses wrong python version, how to change?

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?

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 ... ." 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM