Running and Getting Content of Python File (v9.2)

Hi,

I am trying to get get the content of a csv file, through a python script in to Keyboard Maestro. The python script can be seen in the comment in the file, it works, both in my editor and through the Terminal, however, i get the error below, how is this fixed?

File "/Users/jensbay/Dropbox/#1 Files/Andet/Fun/Sun/get_data.py", line 1, in
import pandas as pd
ImportError: No module named pandas. Macro “Trying” cancelled (while executing Execute Shell Script).

Comment.kmactions (1.6 KB)

Keyboard Maestro 9.2 on macOS 11.2.3

Best Regards
Jens

How are you actually running the Python script?

With the execute Shell script, with the content below:

$(which python) "[full path]"

[action:Execute a Shell Script [Keyboard Maestro Wiki]](action:Execute a Shell Script [Keyboard Maestro Wiki])

are

echo $(which python)

and

echo $PATH

(as the sole contents of shell script actions)

showing the the results you are assuming ?

( a fresh-born vanilla shell will not know where to look for pandas)

I don't know whether this helps:

2 Likes

Thanks - the reading helps! I have a much clearer idea of what I've stumbled my way into.

The two echo statements displays the path, not the output i am looking for

That being said, i've tried to mimick you reply, however i still get one of two errors, see below.

Input in shell script

/usr/bin/python3 "/Users/jensbay/Dropbox/#1 Files/Andet/Fun/Sun/get_data.py"

Error message 1:

2021-04-19 13:54:27 Execute a Shell Script failed with script error: Traceback (most recent call last):
File "/Users/jensbay/Dropbox/#1 Files/Andet/Fun/Sun/get_data.py", line 1, in
import pandas as pd

Error message 2:

Execute a Shell Script failed with script error: text-script: line 2: /Users/jensbay/Dropbox/#1 Files/Andet/Fun/Sun/get_data.py: Permission denied

Input in terminal
python3 "/Users/jensbay/Dropbox/#1 Files/Andet/Fun/Sun/get_data.py"
Output in terminal [which is correct]
[7.56, 13.12]

What happens in the terminal doesn't actually provide any insight into what happens in an Execute shell action.

You will find, for example, that result of

echo $PATH

in Terminal.app

is not the same as the result of the same line in an Execute Shell Script actions.

The two shell instances have no shared history, and no knowledge of each other.

You have to explicitly specify the path to the pandas module, or explicitly add that path to the the PATH variable at action run-time

1 Like

Hey Jens,

Clearly you have not mirrored your shell environment between the system and Keyboard Maestro.

Keyboard Maestro does NOT load any customizations of your environment by default.

You have to do it.

The shell needs to know where the correct version of Python is, and Python needs to know the correct path to find it's modules.

If you've installed Python 3 with MacPorts or Homebrew they have likely altered your ~/.profile file or your ~/bash.rc file.

If you know what file your system is using for these modifications you can do something like this for simplicity:

Execute a Shell Script.kmactions (712 B)

source ~/.profile
python3 ~/'Dropbox/#1 Files/Andet/Fun/Sun/get_data.py'"

This may be able to get you started, but if you intend to run Python from Keyboard Maestro regularly I recommend you figure out how to set up your shell/python environment within KM properly.

-Chris

2 Likes

Hi Chris,

We do agree - however in looking for the bash.rc file I stumbled on the enverionment, and so I solved it!

In short, I just have a lot of python enverionments and so i got lost :man_facepalming:
I'll have to clean that up...

Thanks a lot @ComplexPoint and @ccstone for taking the time!

Best Regards
Jens

1 Like

Great!

What was the solution?

Using the correct path - I've found that I have four separate bins/ python environments... so I just had to use the right one.

So using this "/usr/local/bin/python3" instead of “/usr/bin/python3 ”

The solution would be: be specific in calling the python environment, when using shell scripts.

Best Regards,
Jens

Hey Jens,

What version of macOS are you using?

And what do you get if you run this in the Terminal:

$(whereis python) --version

You have apparently installed a newer version of Python than came with your macOS version – how did you do it?

MacPorts?
Homebrew?
Other?

-Chris

Hi Chris,

I've written the script in Visual Studio Code, and I have Anaconda (for Jupiter Notebook) on my desktop as well, so I've downloaded the extra Python versions through those application

[I don't know if that answered your quetion ?:sweat_smile:]

Best Regards
Jens

By the way, I'm wondering if Pandas is overkill - given the existence of the built-in CSV module in Python.

In most cases this error in Python generally raised:

  • You haven't installed Pandas explicitly with pip install pandas.
  • You may have different Python versions on your computer and Pandas is not installed for the particular version you're using.

So, before being able to import the Pandas module , you need to install Pandas library using pip.

  • pip install pandas

To be sure you are not having multiple Python versions that are confusing, you should run following commands:

  • python3 -m pip install pandas
  • python3 -c 'import pandas'
3 Likes