I am trying to run a Python script from inside Keyboard Maestro. When I run the macro, nothing happens. I created another macro that just shows "Hello World". Nothing happens there, either. After browsing the forum, reading the Keyboard Maestro documentation and chatting with ChatGPT for a long time, I’m still coming up short. Any help would be greatly appreciated. By the way, my macros work fine in an editor, and I have also successfully run them from the command line. I’m on a Mac (Sequoia 15.5), Keyboard Maestro 11.0.4, Python 3.11.5.
I have a feeling that Keyboard Maestro cannot find Python on my system. I have included the paths at the top of the script, but that doesn’t seem to work. Maybe I need something more than this but I don’t know what that is.
2025-06-25 15:47:43 Execute macro “Delete to .?! - Control Punt Python” from trigger The Hot Key ⌃O is pressed
2025-06-25 15:47:43 Action 13765249 failed: Execute a Shell Script failed with script error: /Users/theostar/anaconda3/bin/python3: can't open file '//<<EOF': [Errno 2] No such file or directory
2025-06-25 15:47:43 Execute a Shell Script failed with script error: /Users/theostar/anaconda3/bin/python3: can't open file '//<<EOF': [Errno 2] No such file or directory. Macro “Delete to .?! - Control Punt Python” cancelled (while executing Execute Shell Script).
I looked in the Anaconda folder, and sure enough, there is no python3. There is, however, a python.app and a python3.11
I'm asking if you did type anything like that -- part of the point of using conda is that you can easily set up and switch between different "environments", each with its own python version (and so a different path to the python binary), installed modules, etc.
Go to Terminal and type in
conda info | head -2
...and hit Return. What is the output?
If it doesn't say
active environment: None
...then type
conda deactivate
which python3
...and see if you get a better python path to use in your script -- for example, /opt/homebrew/bin/python3 if you've a brew/Apple Silicon install.
Thank you. I did as you said. And after I typed which python3 in Terminal, I got another path and put #!/Library/Frameworks/Python.framework/Versions/3.13/bin/python3 at the top of my script.
Same result, though. I press the hotkey for the macro and nothing happens.
Make sure you've allowed Notifications for both Keyboard Maestro and Keyboard Maestro Engine (System Settings -> Notifications) so you at least get notified about any errors in your macro.
Don't start your script with #! if you are using a <<EOF "here document" -- your first line should be:
Thank you. That was a very good tip. I enabled notifications and looked at the error messages. The first message was this:
Execute a Shell Script failed with script error: text-script: line 1: /Library/Frameworks/Python.framework/Versions/3.13/bin/python3: No such file or directory. Macro "Delete to .?! - Control Punt Python" cancelled (while executing Execute Shell Script).
So I changed the first line in the script to:
#!/usr/bin/env python3
I ran the script and got the following error message:
Execute a Shell Script failed with script error: Traceback (most recent call last):
File "text-script", ", line 2, in import pyperclip ModuleNotFoundError: No module named 'pyperclip'. Macro "Delete to .?! - Control Punt Python" cancelled (while executing Execute Shell Script).
So it can’t find pyperclip. That is strange. When I run the code in an editor (Coderunner), it works without a hitch.
That is what I reported earlier, as someone who does not currently have Anaconda installed. Since you do have Anaconda installed, and your script works outside KM, I should think that the problem is simply that the script is divorced from the intended path environment and so does not know where to find pyperclip. See the wiki on this, but basically you can either set up the path environmental variable for KM or else use the full path to pyperclip in your script.
Python has changed the rules - you should create so named "virtual environment" for this solution (aka project), install required packages, test and later run activating venv in KM
Assuming that you have installed python (I suggest install from homebrew) and it is reachable in terminal session, it may looks like below
# create place for project and required packages
mkdir -p ~/projects/pyvenvs/pyperclip
cd ~/projects/pyvenvs/pyperclip
# create virtual environment in subdirectory venv
python3 -m venv venv
# activate virtual environment for install required packages
. venv/bin/activate
# install required packages and their dependencies
pip install paperclip
# below test is module available
python3
>>>import pyperclip. # <-- if no error - module is available
>>>quit
# deactivate interactive virtual environment
deactivate
# the project is ready to work
After preparing you may activate this virtual environment for script by pointing in the python interpreter inside virtual environment like (in KM execute shell script):
We've now got a better idea of how your machine is set up.
@nutilius will know a lot more about this than me, but since you appear to be using the base environment you might get away with activating that within your shell script.
This appears to work for me:
#!/bin/bash
# my conda is set up from ~/.bash_profile, so load that
source ~/.bash_profile
# Now that's loaded we can activate the 'base' environment, no name needed
conda activate
# And now, hopefully, we can run our script!
python <<EOF
import pyperclip
x = pyperclip.paste()
lijst = x.split()
x1 = lijst[0]
x2 = lijst[1]
if x1[0].isupper():
x1 = x1.lower()
x2 = x2.title()
lijst[0] = x2
lijst[1] = x1
x = " ".join(lijst)
pyperclip.copy(x)
EOF
Thank you very much for all your help, but this is still not working. It appears to be a lot more complicated than I thought. And I also feel I am in over my head with this. Which is a shame because I have written a lot of code for working with text in Python. This code is working perfectly in the editor, and I would so love to see it work from within Keyboard Maestro. Unfortunately, the one thing I would need for this is to be able to read from and write to the clipboard, and I just cannot seem to get this to work. I'm going to give this a rest for now because I have already spent a few days struggling with this. Thanks again enormously for all your help.
Python can be a pain because of the "modern" way of always running in a virtual environment, plus Python "managers" like conda make alterations to your shell environment that are used by Terminal but not automatically picked up by the KM "Execute a Shell Script" action.
All that makes it very difficult to troubleshoot remotely -- so many things are particular to your setup.
Try going through all your Terminal environment settings -- printenv will list them -- and your conda settings -- conda info to work out how things are being set up in Terminal, and how you can reproduce that in the KM action.