[feature request] execute python script

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.

2 Likes