Home > Net >  Python command in Terminal works perfectly, but through AppleScript it throws an error
Python command in Terminal works perfectly, but through AppleScript it throws an error

Time:09-21

I have a Python script that runs fine through Terminal, but from the moment I try to run the same script with AppleScript (do shell script), I get an error about Python modules that couldn't be found.

More specific:

The script I run refers to a previously installed module, beginning with:

from platform import mac_ver

from Cocoa import NSURL
from CoreFoundation import CFPreferencesAppSynchronize
from CoreFoundation import CFURLCreateWithString
from CoreFoundation import kCFAllocatorDefault
...

While running this in AppleScript, I get an error saying the Cocoa module couldn't be found. I put that line in comment just to test, after which an error about the CoreFoundation module appears. It seems as if the script can't connect with anything that is needed for proper execution.

What could cause this?

Thanks for any info on this!

CodePudding user response:

AppleScript do shell script uses a default shell that has a stripped-down PATH variable. That will make it default to OSX's default Python 2.x install. The easiest solution is to use an explicit path to your install of Python 3.9 (likely something like /usr/bin/python3 or /usr/local/bin/python3).

  • Related