Help With Executing Python Script (3.10)

Hello All,

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.

These are the errors I get:

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/ line 5: import: command not found
/Users/XXX/bin/Weather/ line 6: from: command not found
/Users/XXX/bin/Weather/ line 7: from: command not found
/Users/XXX/bin/Weather/ line 8: from: command not found
/Users/XXX/bin/Weather/ line 9: from: command not found
/Users/XXX/bin/Weather/ line 11: from: command not found
/Users/XXX/bin/Weather/ line 14: syntax error near unexpected token `locale.LC_ALL,'
/Users/XXX/bin/Weather/ 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

Any tips on how to make it work again?


Taking a total punt here, but...

Normally you'd append extra paths to your $PATH variable -- here you're completely replacing it, using only paths in your home directory.

I'm guessing that one or more of the modules you are trying to import exist elsewhere on your drive (from a previous pip install, maybe?) and your script can't find them.

Try comparing $PATH in Terminal to to the ENV_PATH you are setting in KM and seeing if any Python-related entries exist in the former but not the latter.

(Also, the return characters in all 3 look a bit odd. I don't think they're illegal, but I also don't think they actually do anything. Try removing them.)

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/ 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/ line 14: syntax error near unexpected token locale.LC_ALL,' /Users/XXX/bin/Weather/ 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.

And again, apologies for the wasted time...

Nige, thanks again!

But I am still stuck somehow.

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:


So nearly the same but with an empty entry and the last line 2 times. Why?

And this is so with an enabled or disabled ENV_PYENV_ROOT=/Users/XXX/.pyen action.


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

action to your macro, compare the two outputs.

Thank you very much. Now it works.

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?

Thanks again!


I had exactly the same problem with Python3.11 (with a script that works fine with Python3.9) but stumbled over a much simpler "solution".

It turns out that Python3.11 induces Keyboard Maestro to ignore the first line of the text script!

So inserting any line at the beginning, even a blank line, will make it work. Don't ask me to explain how Python3.11 reaches backward in time and space to influence KM, because I haven't a clue.