Having Trouble with Execute Shell Script

This is a total new one on me, and I could use some help.

I have a Python script. From Terminal, I can get the script to run, but I'm not sure how to do it in KM. I've looked around the forum and the wiki and tried a few things, but I'm not really understanding. This is what I've got. What am I missing and/or doing wrong?

This is what I type in Terminal for a successful run of the script. How do I make that work in a KM action?

Following forum post examples and what I think I understand of the wiki instructions, I tried the following (with and without quotes, with set ENV_PATH to the path of the Python script and then the full path in the Execute Shell Script action). The result is always the error shown.

Help?

Screen Shot 2020-09-24 at 09.55.24

"SyntaxError: invalid syntax" for this:

You must have missed how to use variable in a shell script:

image

See Using KM Variables in a Execute a Shell Script action

Thanks, but it doesn't work correctly even without variables. I don't know what I'm doing incorrectly.

You will need to be more specific than "doesn't work correctly" for us to help you.
I'd suggest that you start by uploading your macro that does NOT use KM variables, and then show us what happens where you run it.
A screen capture of the results would be great.

I'm not at all sure what you're up to but it doesn't seem to make sense to use a shebang for python and then try to run python from the command line. I'd expect to see python after the shebang.

Here's a little example of a python script following the shebang that runs reliably on my system. Maybe it will clarify the issue for you:

**Python test Macro (v9.0.6)

Python test.kmmacros (3.4 KB)

The python and python3 commands are not found in that shell action because:

  • you haven't specified a path to them
  • the vanilla shell instance run by KM doesn't inherit any path variables from the instance used by Terminal.app

Depending on your python installation(s), you would need to expand the bare string python3 for example, which is not resolvable by the shell, to something specific like:

/usr/bin/python3

or for 2.7, perhaps:

/usr/bin/python

PS the trick, I find, is always to work incrementally.

Find out how to successfully evaluate and display something atomic, like:

image

and then gradually elaborate, layer upon layer.

1 Like

I did everything you asked for in my earlier comments.

Thank you for the advice. It's brought me closer to having something functional. If I don't use variables, I can get it to work.

Adjusting for my path to Python, @ComplexPoint 's example worked.

@mrpasini 's example also worked, but I can't seem to figure out how to adapt it to my need.

Running this (note: no variables) returns a successful result.

But running this...

...fails with this log entry:

SyntaxError: invalid syntax
2020-09-28 16:54:36 Execute a Shell Script failed with script error: File "text-script", line 6
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" + n
    ^
SyntaxError: invalid syntax. Macro “Trying” cancelled (while executing Execute Shell Script).

Similarly these...

Fail with KM engine log entries like this:

SyntaxError: invalid syntax
2020-09-28 17:00:12 Execute a Shell Script failed with script error: File "text-script", line 4
    write /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" $KMVAR_kmVar
                                                                                                                                                    ^
SyntaxError: invalid syntax. Macro “Trying” cancelled (while executing Execute Shell Script).

I tried putting the variable in double quotes per https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script#Passing_Paths_in_Variables

but I get the error

SyntaxError: invalid syntax
2020-09-28 17:02:23 Execute a Shell Script failed with script error: File "text-script", line 4
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/pariah/Dropbox/_Crucial Files/System - Mac/Mixcloud_Uploader/main.py" "$KMVAR_kmVar"
    ^
SyntaxError: invalid syntax. Macro “Trying” cancelled (while executing Execute Shell Script).

OK then. I am out of ideas.
I think @mrpasini has given you some great advice and suggestions.
I'll leave it up to you guys to resolve this if needed.

With that hash bang and import os you invoke evaluation of the string as Python source.

Which is fine, but then that filePath string (starting with /Library) is unquoted, so it doesn't constitute well-formed Python, and chokes the interpreter.

(The shell interpreter understands bare paths, as long as they contain no spaces, but the Python interpreter doesn't)

I don't understand, @ComplexPoint . Could you please show me what I need to type to get it to work?

I personally think the simplest and most direct approach is that of the my earlier post:

(Not sure that I can immediately see the advantage, in this case, of additionally wrapping it in a hashbang script, and having to disentangle shell processing from Python processing)

I don't understand the terms "hashbang script" and "disentangle shell processing." I know KM, not scripting. Someone wrote a Python script for me (and I don't know how it works or even how I installed Python). Now I need to execute that script via KM. It works without variables. How would I write the shell script to make it work with the one variable .yaml file path I show in my examples?

Then do not use my approach. It assumes you want to work directly with Python code.

You want to run Python from the command line. ComplexPoint has shown you how.

I wish that was the case, but I don't see it. @ComplexPoint presented a sample path for Python that doesn't work on my computer with or without variables. Again, I don't understand the phrases ComplexPoint used.

I have it running from the command line when I type it into Terminal. I also have it running without a variable in KM as a Shell Script action. I just can't get it running with the .yaml file path variable.

Could you please tell me what I need to write to make it work?

ComplexPoint addressed that. Maybe this discussion will help:

How to Escape Spaces Etc In When Passing Variable to Shell