Can't Execute a Python File

I am using a python file.

I have 2 different way to execute it:

By using the Python interpreter, or by typing in the Terminal:

python 3 <path to python file>

But when I try to do it with Keyboard Maestro in an Execute Shell Script action it doesn't work.



Hi @Daniel2344,

Do you have python interpreter set up in the ENV_PATH variable in Keyboard Maestro?
From action:Execute a Shell Script [Keyboard Maestro Wiki]:

like this?

it doesn't work

i create the Variable

what now

If, in Terminal.app you enter a command like:

which python3

you should see the full installation path. Perhaps, for example something like:

/usr/local/bin/python3

Whatever the full path is, use it in your Execute Shell Script action, instead of just writing python3

(Execute a Shell Script actions launch a vanilla shell, which doesn't automatically receive any search paths from the Terminal.app instance of the shell)

i been typing in terminal: python 3, than the path of the python file
like:
python 3 /Users/guyatia/PycharmProjects/pythonProject1/main.py

like this ?

What works in the terminal is not relevant to what works in an execute shell script action – they are different environments.

If the which command shows that your python3 is installed at:

/usr/local/bin/python3

then something like:

/usr/local/bin/python3 /Users/guyatia/PycharmProjects/pythonProject1/main.py

Ok
i am reading about which command

can you tell me in which way i know if
the which command shows that your python3 is installed at:/usr/local/bin/python3
what code in need to write in the terminal for this

the terminal say :
Python 3.9.7 (v3.9.7:1016ef3790, Aug 30 2021, 16:39:15)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information."

Hey Daniel,

Type:

which python3

In the Terminal and hit the return key.

Funaho:test_directory myUser$ which python3
/opt/local/bin/python3

Alternatively you can use:

type python3
Funaho:test_directory myUser$ type python3
python3 is /opt/local/bin/python3

The path returned shows the location of the python3 executable.

To execute your python program use the following format with YOUR paths:

/opt/local/bin/python3 <path to your python file>

NOTE – if your python program is has dependencies on python 3 modules it's probably not going to work and will require some more configuration of your system.

-Chris

I knew nothing about environ path when I started to learn programming languages. It took me a long time to understand the environ path.
I'll try to describe how I understand it.
When we use a command in the terminal, say python3 /path/to/file.py. On the mac, if the default language is zsh, zsh has to know where python3 (called interpreter) is and asks it to execute the scripts.

When we run which python3, if python3 is installed and zsh knows where it is, the which python3 command will return the path to python3, something like /usr/local/bin/python3.
If python3 is installed but zsh does not know it, then you will have to know where does zsh searches for python3. Here, environ path comes in.
If we run echo $PATH in the terminal, we will get a serial of directories separated by :. zsh search search these directories, looking for python3, if it finds it, then it will ask python3 to run the script. (if you have multiple python3 interpreters, the first that is found will be used as the interpreter. Therefore, the order of the directories matters). Otherwise, it will say that the interpreter is lacking. We will have to add the path to python3 to it by editing a file named .zshrc (for zsh).

When we run python scripts in Keyboard Maestro, we are not in the Terminal anymore. Therefore, the .zshrc file is irrelevant. We have to let Keyboard Maestro know where the python3 interpreter is. We do this by creating a Keyboard Maestro variable called ENV_PATH. Keyboard Maestro will search through these directories and look for python3.

If the ENV_PATH is properly set, we can use

python3 /path/to/file.py

in the Shell Script action to run the script.

If the ENV_PATH is not set, we have to use the full path to python3 instead of python3, that is, we have to use something like:

/path/to/python3 /path/to/file.py
1 Like

thanks for your explinasion

so now
how can i know the path to ```
python

I haved answered here:

see also

here i did it :slight_smile:



he say:
The default interactive shell is now zsh.