I searched and tested, but could really not find a solution for this.
I have a Python script started from Keyboard Maestro which worked for years. One day it broke after a Python update, and I needed to fix a few things – then it again worked both from terminal as well as from KM. Now it still works from terminal but no longer from KM.
The first few lines, that are already enough to throw errors (from KM):
import json, subprocess, re, os, locale, time, sys, codecs, math, pygal
from decimal import Decimal as dec
from datetime import datetime
from string import Template
from urllib.request import urlopen
Python is 3.10.4
KM is 10.1.1
macOS is 11.6.2.
This is the action where I already tried to play around with ENV_-variables, but no avail.
2022-07-10 23:16:40 Execute macro “3 Wetter” from trigger Duplicate Macro Palette
2022-07-10 23:16:40 Action 121 failed: Execute a Shell Script failed with script error: /Users/XXX/bin/Weather/forecast.py: line 5: import: command not found
/Users/XXX/bin/Weather/forecast.py: line 6: from: command not found
/Users/XXX/bin/Weather/forecast.py: line 7: from: command not found
/Users/XXX/bin/Weather/forecast.py: line 8: from: command not found
/Users/XXX/bin/Weather/forecast.py: line 9: from: command not found
/Users/XXX/bin/Weather/forecast.py: line 11: from: command not found
/Users/XXX/bin/Weather/forecast.py: line 14: syntax error near unexpected token `locale.LC_ALL,'
/Users/XXX/bin/Weather/forecast.py: line 14: `locale.setlocale(locale.LC_ALL, 'de_DE')'
2022-07-10 23:16:40 Action 122 failed: Execute a Shell Script failed with script error: text-script: line 1: open: command not found
Nige, thanks, that (expanding ENV_PATH to the whole $PATH of the shell (and deleting the "returns")) has, at least brought me a step further, but I am still not at a running configuration.
The errors now are:
2022-07-11 14:19:27 Action 121 failed: Execute a Shell Script failed with script error: /Users/XXX/bin/Weather/forecast.py: line 5: /usr/local/bin/import: Bad CPU type in executable
from: can't read /var/mail/decimal
from: can't read /var/mail/datetime
from: can't read /var/mail/string
from: can't read /var/mail/urllib.request
from: can't read /var/mail/lib.ytranslate
/Users/XXX/bin/Weather/forecast.py: line 14: syntax error near unexpected token locale.LC_ALL,' /Users/XXX/bin/Weather/forecast.py: line 14: locale.setlocale(locale.LC_ALL, 'de_DE')'
I have no idea, why now /var/mail is in the modules search path and what bad CPU-type means.
If it matters, I am running an Intel CPU. "iMac (Retina 5K, 27'', ultimo 2014)"
I may have given you a bum steer earlier. While I said about $PATH that "you'd normally append", I didn't spot you are using pyenv. According to the docs that works by prepending the shims directory to your path -- $PATH is searched in order, first hit is used, so we're now "redirecting" to an old 32-bit executable and getting the "bad CPU" message (he guessed, wildly!).
So, going by the same theory that "if it works in Terminal we just need to recreate the Terminal ENV in KM", make sure that the ENV_PATH you are creating has the same items, and in the same order, as Terminal's echo $PATH output.
If setting the ENV_PATH same as my $PATH and replacing my script start by a simple "python --version" I do get "Python 3.10.4" without the ENV_PATH I do get "Python 2.7.16". So setting the path was the first step.
Next thing I did was to write a litte script to show me sys.path: #!/Users/XXX/.pyenv/shims/python3 import sys for path in sys.path: print(path)
Which brings up, when started from the shell:
Started from KM:
Not a clue! And I don't know if it even matters. I know next to nothing about Python, not much more about KM, and have never used pyenv...
But, as in all cases where things work in one place but not another, look for differences. If you're sure you've got PATH sorted (and that's worth double-checking) turn your attention to the rest of the environment. Do a printenv in Terminal, add a
One thing was the (now full) KM variable ENV_PATH, since without it the wrong Python got pulled.
The difference is that my "Execute text script" now includes the shell it should run under.
So instead of just:
It now is:
And a quick test by removing the #!/bin/zsh part confirms this. Also using bash does not work and I get the same errors in the Terminal using bash. So I guess my Python installation/environment is somehow bound to zsh.
So it had not too much to do with Keyboard Maestro, BUT, shouldn't KM use the Logon-Shell?