Need Help Running Python Shell Script from KM

Hi there. I am really frustrated because something that should be really simple has turned into a little nightmare.

I have a python script that takes the contents from the clipboard, processes them, then does a search through some texts with this input and yields a result in the form of new clipboard contents.

The script works perfectly when executed from the terminal:

$python3 script.py

Since the initial input is obtained by selecting text and copying from a word document, I thought KM would save me the work of having to first copy the text, then executing the script and then pasting the results in my document. The macro when triggered should simply copy the text I have selected and then execute the python script. I had tried something fancier which involved KM handling the whole process but I realized it was much simpler for me to use a Python module that gets input directly from whatever is in the clipboard and copies the result to the clipboard again.

OK, here is a screenshot of what I have done. It looks like KM does not execute the script but the weird thing is that it doesn't throw any error message either. If anybody can lend me a hand with this, I would really appreciate it.

Hi, Josep

Presumably, the trigger works and the KM log shows that it's run?

tail -f ~/Library/Logs/Keyboard\ Maestro/Engine.log

Shouldn't the spaces in the path to the script be escaped? The log (and a pop-up notification) would show if the script wasn't being found.

Does the script depend on any environment variables or a particular Python environment that KM won't supply by default?

Best wishes,
Steve

what happens if you use the full path to the python script instead of just the filename?

When you are doing this from Terminal, what directory are you in? I'm guessing $HOME?

When you launch a script from Keyboard Maestro, the PWD/CWD is /.

So try

/path/to/python3 "$HOME/cite_examples.py" 

Thanks to everybody who responded. One clarification. My screenshot did not represent accurately what I did. I had started to modify it before and I got distracted. Then I decided to write a message here and didn't remember I had changed stuff.

As Steve noticed, the spaces were not escaped. I had that problem the first time I tried but that was solved. kvanh, the command was actually with the full path to the script:

python3 /Volumes/GoogleDrive/My\Drive/DOCS\PERMANENTS/PycharmProjects/PYTHON_SCRIPTS/cite_examples_imac.py

not

/Volumes/GoogleDrive/My\Drive/DOCS\PERMANENTS/PycharmProjects/PYTHON_SCRIPTS/python3 cite_examples_imac.py

as in the screenshot.

tjluoma, in principle I can execute python3 from any directory so I thought executing it from KM would work as well. I can do $python3 path/script.py from anywhere.

Here's what I see in Engine.log

2021-07-07 19:08:50 Execute macro “cite_examples” from trigger The Hot Key ⌃⌘Right Arrow is pressed
2021-07-07 19:08:51 Execute a Shell Script failed with script error: text-script: line 1: python3: command not found
2021-07-07 19:08:51 Execute a Shell Script failed with script error: text-script: line 1: python3: command not found. Macro “cite_examples” cancelled (while executing Execute Shell Script).

I'm don't really know what to make of this.

tjluoma, my scripts live in a Google Drive folder. The path to the script is the one indicated above. I've made a copy of my script in my home folder and I've tried to do what you suggest in two different ways:

/usr/local/bin/python3 ""/Volumes/GoogleDrive/My\ Drive/DOCS\ PERMANENTS/PycharmProjects/PYTHON_SCRIPTS/cite_examples_imac.py"

and

/usr/local/bin/python3 "$HOME/cite_examples.py"

I still get this error:

2021-07-07 19:45:07 Execute macro “cite_examples” from trigger The Hot Key ⌃⌘Right Arrow is pressed
2021-07-07 19:45:07 Execute a Shell Script failed with script error: /usr/local/bin/python3: can't open file '/Users/Hal-UPF/cite_examples.py': [Errno 2] No such file or directory
2021-07-07 19:45:07 Execute a Shell Script failed with script error: /usr/local/bin/python3: can't open file '/Users/Hal-UPF/cite_examples.py': [Errno 2] No such file or directory. Macro “cite_examples” cancelled (while executing Execute Shell Script).

I hope you don't mind that I have revised your topic title to better reflect the question you have asked.

FROM:
Simple execution of a script is mission impossible

TO:
Need Help Running Python Shell Script from KM

This will greatly help you attract more experienced users to help solve your problem, and will help future readers find your question, and the solution.

As noted above, KM runs shell scripts in a different environment than Terminal, which is well documented at Execute a Shell Script action.
In particular there are sections in that Wiki Article about Python and Paths that are well worth reading.

I see that several of our shell script gurus have engaged in helping you, so you are in good hands.

If my understanding is correct, we can use either the double quote or the escape to include the space in the path.

E.g.,

"/path to/file.py"
or
/path\ to/file.py

I saw you used both. Also, I don't know why there are two quotes in the beginning and one at the end.

Yes. That was a typo. I fixed it and it still didn't work.

Thanks JMichaelTX. No problem with changing the topic title if it can help other people find this thread and I can get more people to lend me a hand.

OK. Now I have read carefully all of the information from the links you provided and I can understand what my basic problem was.

I have been able to solve the problem by Setting the ENV_PATH Keyboard Maestro Variable with my $PATH so thank you for that.

I haven't been able to make it work with the other option, though, i.e. to set the $PATH environment variable within each Execute Shell Script action.

Perhaps I misunderstood something but what I was doing was the following. In the execute text script window I wrote:

#!/usr/bin/env zsh -f

export PATH=$PATH:/Contents_of_echo $PATH

python3 /Volumes/GoogleDrive/My\ Drive/DOCS\ PERMANENTS/PycharmProjects/PYTHON_SCRIPTS/cite_examples.py

When I do this things obviously improve from the initial situation because now the logs show the script is actually executed but I see in the error message that one of the modules I use in my python script appears as not installed ("No module named 'clipboard' "). This module is installed via pip and the script works when I call it from the terminal.

What appears to be happening is that when KM executes the script, it does it using a different python environment from the one where I installed this module. The default Mac Python installation is pretty useless and so almost everybody installs newer versions of Python via Homebrew or Conda or in other ways.

What I don't understand is why would exactly the same $PATH environment would work when set it as the ENV_PATH Keyboard Maestro Variable but not as the $PATH environment variable within the Execute Shell Script action.

2 Likes