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.
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
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
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
with a properly configured ENV_PATH variable?
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.
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:
EDIT: You could also try running the script file directly from the Execute Shell Script action and see if that makes a difference:
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:
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.
Actually I spoke too soon. I can run simple python scripts with KM but when I try to run something quite complex it breaks down. KM cannot import certain modules. So I call one module which uses xlwings. When KM tries to import xlwings it has trouble importing the modules that xlwings imports. Look at this
from main import *
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/proofs/other/activities/main.py", line 6, in
import xlwings
File "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/xlwings/init.py", line 25, in
from . import _xlmac as xlplatform
File "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/xlwings/_xlmac.py", line 10, in
import aem
ModuleNotFoundError: No module named 'aem'
So you can see that it successfully executes
from main import *
which is my code. Then it imports xlwings. After that it goes into another module which is not my code. This is where things start to go wrong. It executes line 25 of in the xlwings module but then it cannot execute line 10 of the xlmac.py module. Really at a loss here.
Ok, I've made a little bit of progress. I encounter versions of this error when I use terminal and I solve it by doing this:
[python]
import sys
str1 = "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/"
sys.path.append(str1)
[/python]
When I remove that code I get the following error:
[python]
Traceback (most recent call last):
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/main.py", line 6, in
import xlwings
ModuleNotFoundError: No module named 'xlwings'
[/python]
So I'm on the right track. I just need to find out what path to append to sys so that I do not get this error:
[python]
Traceback (most recent call last):
File "/Users/kylefoley/PycharmProjects/inference_engine2/inference2/Proofs/other/activities/main.py", line 6, in
import xlwings
File "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/xlwings/init.py", line 25, in
from . import _xlmac as xlplatform
File "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/xlwings/_xlmac.py", line 10, in
import aem
ModuleNotFoundError: No module named 'aem'
[/python]
So after I found the module 'aem' I put in the following code:
[python]
str1 = "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/aeosa/aem/"
sys.path.append(str1)
[/python]
That didn't work however. I'm getting the same error message.
Current problem solved
it should be
str1 = "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/aeosa/"
not
str1 = "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/aeosa/aem/"
In the Terminal, run the command:
env
In there you will likely see some PYTHON related variables. These variables are configuring how python behaves. You will also see the PATH variable which controls where it finds commands to run, ad in your case will include the path to python3.
In Keyboard Maestro, in the Preferences, Variables pane, set the corresponding ENV_ variables (eg ENV_PATH, and ENV_PYTHONLIB (or whatever the PYTHON variables are) to match your Terminal env
results.