KM and Python

Hi,
I am still doing a lot using KM and Python/Apple script but now I am trying to execute a Python-script from an action in KM. But it doesn't execute. And now - myself and Mr chatGPT have spent 10 hours trying to make it work (yes:chatGPT has disappointed me massive on this).

Hi again,
I am adding some extra info:

Buffer_PostFraQueue.kmmacros (42.3 KB)

Archive.zip (2.2 KB)

content of the variable: RabbitScriptOutput:
test_py.txt.zip (718 Bytes)

There may be an issue with the environment used and the modules/packages your script tries to use.

As @ccstone suggested in another thread, use his macro to
Check your actual $PATH differences between the Terminal and KM

From a quick scan of your shell actions, you need to use the correct syntax to use variables.
image

"%Variable%RabbitScriptOutput%"

"$KMVAR_RabbitScriptOutput"

You can read and also even copy Keyboard Maestro documentation and include in the context of your ChatGPT prompt:
https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

See: Execute a Shell Script action.

In your Terminal, start with which python and ensure you are using the same version of python when executing your script from Keyboard Maestro.

If there are any environment variables which control the execution of your python script, including the PATH environment variable, then remember they will be absent or different in the Keyboard Maestro script and you will need to allow for that or configure them as described., eg:

For example, if you want to use Perl with a custom library search path in the Execute a Shell Script action, you can also create a Keyboard Maestro variable ENV_PERL5LIB that will set the PERL5LIB environment variable.

Also, don't use --- in your posts as it turns everything above it in to a heading which is generally not desirable.

or perhaps, these days:

which python3
1 Like

I create virtual environments then run scripts with the full path to python inside the virtual environment - without activating it.

I also make the script load any environment variables if need be.

This method also gets around the macOS restrictions on adding new python packages.

1 Like

Any pointers to details of your approach ?

Create a virtual environment in your project root folder

python3 -m venv venv

The second venv could be anything - I used to use env - it's just a standard naming convention.

To activate it in normal development mode

source venv/bin/activate

You can now add any packages you want.

The path to python is now relative.

Before activation of virtual environment on my device

which python3
/opt/homebrew/bin/python3

Activation

csmu@macmini Hub % source venv/bin/activate

Path after activation

(venv) csmu@macmini Hub % which python3           
/Users/csmu/source/Hub/venv/bin/python3

This is the path I use - without the source step.
/Users/csmu/source/Hub/venv/bin/python3

Where I need environment variables I store them and load them - you might be able to load them from your startup files.

Environment files are simply key value pairs
Normally

KEY1=value1
KEY2=value2

Make sure there are no spaces on either side of the equals sign

To load environment files in my python scripts when I nead them I use python-dotenv

pip install python-dotenv

from dotenv import load_dotenv
import os

# Loads variables from a .env file into os.environ
load_dotenv()              
# by default looks for ".env" in the current dir
# or: load_dotenv("/path/to/.env")

db_url = os.getenv("DATABASE_URL")
print(db_url)

I have a script that runs every night for the last five years using these techniques.

2 Likes

See:

In general to run venv from KM script you must use full path to python interpreter existing inside created venv.

1 Like

Hi again,
I have tried several things of the above and other advices but it just doesn't play.
Can I hire any of you to help me out so I can move on with my project?

I will try to help you.

I will check your attachments in the evening (I’m from Poland, my local time is CEST - now is 11:00 AM). If you have other details or proposal, write to me directly.

Which section doesn't work for you? What is the behaviour of script? I need some point to start diagnosis.

In generał maybe short description of task - what script has to do - will give an idea what should be done.

I did first stage on simple data file and simple python script like below - works good.

The problem in your stage was using "%Variable%RabbitScriptOutput%" as argument to the script. You should use KMVAR_RabbitScriptOutput as in example.

href="https://forum.keyboardmaestro.com/uploads/default/original/3X/7/b/7bac8697c5f86dfb76ba885f1af091caa77831b7.kmmacros">Test 01.kmmacros (3.2 KB)

Python code (test1.py):

import os
import sys

if len(sys.argv) < 2:
    print("Need one argument!")
    sys.exit(1)

with open(sys.argv[1], "r", encoding="utf-8") as inp:
    for line in inp:
        try:
            tag, teaser, post_url, gruppe_url, gruppe_type = [x.strip() for x in line.split(" | ")]

        except:
            print("Error")
        print(f"::KMVAR_RabbitScriptOutput_tag::{tag}")
        print(f"::KMVAR_RabbitScriptOutput_teaser::{teaser}")
        print(f"::KMVAR_RabbitScriptOutput_postURL::{post_url}")
        print(f"::KMVAR_RabbitScriptOutput_gruppeURL::{gruppe_url}")
        print(f"::KMVAR_RabbitScriptOutput_type::{gruppe_type}")
        print(f"::KMVAR_RabbitLastPostFile::{os.path.basename(sys.argv[1])}")
        break

File with test data (test1.txt):

Ala | ma | kota | w4 | w5

I'm not sure why you use break after first parsing in your python, but maybe you need only one line.

Anyway please tell me what doesn't work in your case.

BTW
Analysing your KM script I have feeling, that all that thing could be executed in simpler form (call it intuition). Some part of KM could be probably moved to python (using regex and decision table/dictionary).

2 Likes