Applescript call python failed

Question:

I have developed an AppleScript which needs to call a python file. i.e autorun.py the Autorun.py start with

import msoffcrypto
import pathlib
import os
....

Both the AppleScript and the python file run fine. I even tried to call autorun.py in the terminal and that also runs with no problem. But when the Applescript tried to call the python file:

set myPythonScript to POSIX path of "/Users/zhouyu/Library/Application Scripts/com.apple.mail/autounlock.py"

set myVal to do shell script "python" & space & myPythonScript's quoted form

display dialog myVal

It failed at the first line in the python code when Applescript tried to call it.

error "Traceback (most recent call last):
File "/Users/zhouyu/Library/Application Scripts/com.apple.mail/autounlock.py", line 2, in
import msoffcrypto
ImportError: No module named msoffcrypto" number 1

Asked By: fishjoe

||

Answers:

Unlike Terminal.app, do shell script does not read your shell profile so make sure you give it the full path to your python interpreter, e.g.:

do shell script "/usr/local/bin/python3" & space & myPythonScript's quoted form
Answered By: has
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.