Thursday, October 27, 2022

[SOLVED] Applescript call python failed

Issue

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


Solution

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
Answer Checked By - Timothy Miller (WPSolving Admin)