X = os.environ["KMVAR_searchTerm"] stops finding the Keyboard Maestro variable

My macro suddenly stopped working. It has worked wonderfully for weeks. It now throws an error stating it sees the Keyboard Maestro variable as empty (None). Am I not calling a Keyboard Maestro variable correctly in python? Is there some preference setting that I might have inadvertently changed? I tried running this with an older version of python, thinking that maybe an update of python had broken this, but no joy.

Within the Keyboard Maestro macro, I call a python script. In it is a call to get a Keyboard Maestro variable.

import os
x = os.environ["KMVAR_searchTerm"]
print(x)

The searchTerm variable is set

The error I get when I run this code is
Traceback (most recent call last):
File "/Users/will/Dropbox/Projects/playground/intro.py", line 3, in
x = os.environ["KMVAR_searchTerm"]
File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 679, in getitem
raise KeyError(key) from None
KeyError: 'KMVAR_searchTerm'

Keyboard Maestro error just traces back to python line 2.

In another thread you contributed to, this was suggested:

which looks a bit different to what you're saying is now no longer working. Maybe that's the answer? I know nothing about using python together with KM so excuse me if I'm on the wrong track - just trying to be helpful!

1 Like

I see from your error message that the Python script that's causing the error is called intro.py. Are you running it from Keyboard Maestro or straight from the Terminal? If the latter, that's the reason it doesn't work. As best I can tell, Keyboard Maestro variables are not available from a shell script unless that shell script has been called by Keyboard Maestro itself.

You can't, for example, just open a Terminal window, type

echo $KMVAR_searchTerm

and get the result "McCrea Place Fiction History Novella." You'll get nothing.

More generally, running this Python script from the Terminal

import os

kmvars = [ x for x in os.environ.keys() if x.startswith('KMVAR') ]
print(kmvars)

will return an empty pair of square brackets, [], because unless the script is running within the Keyboard Maestro environment there are no environment variables that start with KMVAR.

If you are running your intro.py script from within Keyboard Maestro, then I don't understand the error, and nothing I said above will help you.

1 Like

Thank you for being so helpful. Yes, you are right, and I knew this from your earlier tip. It works when run from within the Keyboard Maestro environment and not in the command line space.

image

My earlier message was a mistaken attempt to simplify the macro to only what I saw as the problem. I see now that the problem is between my poor but growing understanding and my python skills.

What got me off-track was that my script used to run fine if a Keyboard Maestro variable was blank, and now I'm getting an error. Troubleshooting, I see that Keyboard Maestro is working fine. My problem is with my python program. I need to figure out a way that the script will ignore when the user only uses two of the three user inputs, which used to work but now kicks an error.

The culprit, Keyboard Maestro macro, prompts the user for up to three search terms and then searches each line in each file in a directory, looking for lines that contain all the search terms.

Near Search.kmmacros

Thanks for the tip about listing all the KMVARs. It produces an interesting list, many I recognize and some I don't. I'll look at extracting the key, value pairs in a table form just for the exercise.

Hey Will,

Keep in mind that just because a variable is shown in the Keyboard Maestro Editor's variables panel does not mean it exists.

An empty variable in Keyboard Maestro by definition does not exist.

Run this AppleScript to see what variables actually exist:

tell application "Keyboard Maestro Engine"
   set vnList to name of variables
   return vnList
end tell

I don't know how Python handles a call for a nonexistent object.

-Chris

It throws an error. Specifically, a KeyError when you try to access os.environ['KMVAR_EmptyVariableName']. Basically, it acts the same way AppleScript does.

1 Like