How to Call Python in an Execute Shell Script Action

I have a code works well in Python IDLE (the code is for write info to docx).

I want run these code in shell script of KM, how can I call Python in shell script macro? I search many ways, but no one works.

Here is the Python code:

from docx import Document
from docx.shared import Inches
document = Document()
p = document.add_paragraph('Picture bullet section', 'List Bullet')
p = p.insert_paragraph_before('')
r = p.add_run()
r.add_picture("/Users/som/Desktop/aaass.jpg")
<docx.shape.InlineShape object at 0x107fbb8e0>
p = p.insert_paragraph_before('My picture title', 'Heading 1')
document.save('demo_better.docx')

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script#Path_in_Shell_Scripts

I use an action like this to set mine, but you can manually do it as well.

And then call python like normally at the head of your script.

#!/usr/bin/env python

Thanks for your reply, here is my mine, do you know where is wrong?

I'm not a python expert, but the way I was able to import python modules so they work in keyboard maestro's shell environment is by adding the following lines:

import sys
sys.path.append('<PathToPythonModule>')

Check your actual $PATH differences between the Terminal and KM.

Don't call python (you don't have installed or aliased, hence the "not found"), call python3 instead.

So the first line of your main script should be

#!/usr/bin/env python3
1 Like