[feature request] execute python script

I have a python script I want to execute. What can I do?

1 Like

Search the forum for Python. There’s plenty of examples.

Yeah, thought I can just run a bash β€˜python’ command on python file, I just thought it would be also nice to have it in-app like β€˜execute swift script’.

But yeah running a bash command on a python file would work.

Thank you.

It has been asked for before, so I know it’s on the list. Probably low on the list, but on the list nonetheless. :slight_smile:

You could certainly wrap the bash line in a custom add-in action, but given that python scripts often make use of command line switches, an Execute Shell Script action seems quite a useful and flexible way to do it …

There is no explicit Execute Perl/Python/Bash/Tcsh/Ruby/etc action because they are all the Execute Shell Script action.

Assuming your shell script starts with the normal #! (eg #!/usr/bin/python or whatever), then you can execute it with the Execute Shell Script, either as text or a file.

5 Likes

I'm trying run a simple python script on a mac. When I run the python script on terminal I have to write:

python3 /Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/bd.py

with keyboard maestro I tried the following

import os
os.system("""osascript -e 'tell application "Keyboard Maestro Engine" to do script /Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/bd.py'""")

But nothing happened

1 Like

Your script probably ran, but you're not going to see it, unless the bd.py script runs for many seconds. os.system() runs a shell command, then closes as soon as it's over.
If you want to pause your script so you can se it run, put input("PRESS ANY KEY TO CONTINUE") as the last line of your python script.

A better way to run external programs from python scripts, is to use the subprocess module. It provides you with the tools to capture the output of the program you ran, as well as the return code (if any)

I can't use the subprocess module because KM allows me to run my python scripts with the click of one button. Whereas with terminal I have to first bring terminal to the front then type in a two letter alias, that's four button clicks, plus the first button takes a while to press. I know the python script is not running because it just makes a message box pop up and that msgbox is not popping up.

Just tell me how to run a python script from KM, please.

I made some progress. The script runs in terminal but when I use it KM I get an error. I can't read the error message because KM cuts it off, like so:

And here I actually got the python script to run

16%20PM

Just to make sure: have you tried using this command

python3 /Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/bd.py

in an Execute a Shell Script action

image

with a properly configured ENV_PATH variable?

1 Like

I'm not exactly sure what a ENV variable is. What I would really like is to be able to read those error messages. Seems like a very basic thing. One of the scripts is called but it gets an error on line 2. I think it's because KM is a different directory because that error message seems to be like one I get sometimes with terminal and it happens because one of the files in the script does not have the path that the terminal is run from. Also I did read up on ENV's here

But not sure what you're getting at.

The ENV_PATH variable I mentioned is one specific to KM. I included the link in that post, but here it is more prominently:

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script#Path_in_Shell_ScriptsοΏΌ

As for the error, that can admittedly be frustrating when things don't go right in KM, but fortunately, @JMichaelTX has written a macro designed to let us read the whole error message in a much easier format than searching for it in the log file:

This documentation doesn't tell me how to set the path ENV variable it just tells me to do it.

In any case, I think what's going on is that KM is putting /bin/sh: in front of the path of the file. If I could figure out how to delete /bin/sh: then I think the script would run.

Use an explicit, full path to the tool or file.
See Passing Paths in Variables in the above section.
OR
Set the $PATH environment variable within each Execute Shell Script action.
OR
Set ENV_PATH (the Keyboard Maestro Path Variable) prior to the Execute Shell Script Action.
The path defined in this Variable will automatically apply to every Execute Shell Script, without need for further reference. It will work just like your $PATH works in the Terminal app.
You can set this Variable manually in the Variable Preferences Panel of the Keyboard Maestro App Preferences.

From the wiki page:

You can create the ENV_PATH Variable one time using the Variable Preferences Pane of the Keyboard Maestro Preferences.

  • Add a variable named ENV_PATH and set its value.
  • Once set, it will remain unchanged until you change or delete it.

That Variable Preferences Pane link in turn links here:

https://wiki.keyboardmaestro.com/manual/Windows#Preferences_Variables_Pane

Which shows how to manually add a variable in KM's preferences, using ENV_PATH as an example.

1 Like

I set the ENV as /Users/kylefoley/

then I changed the path of my file to

/PycharmProjects/inference_engine2/inference2/Proofs/other/practice4.py

hoping that they would combine to form the right path. I do not get the old error message but now I just get

MACRO ERROR  on  2018-08-29 00:49:09

This info was pulled from the KM Engine Log for the
Last Macro error that occured.

Macro: Execute a Shell Script
Action: Execute β€œpractice4.py” Shell Script
Status: cancelled

ERROR:
––––––––––––––––––––––––––––––––––––––––––––––––––
Execute Shell Script Exception
––––––––––––––––––––––––––––––––––––––––––––––––––

Log File: ~/Library/Logs/Keyboard Maestro/Engine.log

I admit I'm not a shell script expert, but I don't believe that your users folder will help you to run this script. Try setting ENV_PATH to

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

instead of your users folder, and then try running the full command that you know works in Terminal from the Execute a Shell Script action again, i.e.

python3 /Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/bd.py

I would also try displaying the untrimmed results of the script in a window and displaying errors, like so:

03%20PM

EDIT: You could also try running the script file directly from the Execute Shell Script action and see if that makes a difference:

1 Like

Actually I was running it directly. I get the same error. When I run it as a text file I get a different error message. What pops on the screen is different from the macro you provided

MACRO ERROR  on  

This info was pulled from the KM Engine Log for the
Last Macro error that occured.

Macro: Unknown
Action: Unknown
Status: ABORTED

ERROR:
––––––––––––––––––––––––––––––––––––––––––––––––––

––––––––––––––––––––––––––––––––––––––––––––––––––

Log File: ~/Library/Logs/Keyboard Maestro/Engine.log

Here's what pops up on the screen:

52%20AM

Why not just:

  • Execute a Shell Script β€œpython3 /Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/bd.py”

Or if python3 is not in your path as Keyboard Maestro see it, then in the Terminal run the command which python3 and that will give you the full path to python3, and then run the command:

Execute a Shell Script β€œ/path/to/python3 /Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/bd.py”

You can read the full text of any error message in the Engine.log file (Help ➀ Open Logs Folder), and make sure (at least when testing or having problems) that the Execute Shell Script is configured to display the result in a window so you can see any error message.

thanks, that did it.