Can't Execute a Python File

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.

if which python3 does not return the python path, it means your zsh does not know where it is, which means, either you do not have python3 installed or python3 is not in the environ path directory.

If you do have python3 installed, you will have to find out where it is and add its path to the environ path variable. Where python3 is depends on how we install it.
For instance, homebrew installs packages under the /usr/local/Cellar/ directory. For python3, it will be something like /usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/bin/python3.9.

A convenient way to find out the installed python versions is to use the applescript library window (I discovered it accidentally). For instance, I have four python versions installed, one came with MacOS (2.7), one came with Xcode (3.8.9), and two were installed with homebrew (3.9 and 3.10).

homebrew also creates symlinks for packages in the /usr/local/bin folder (it's more complicated and I still need to learn more. for more info., see, e.g., this post). Therefore, we don't need to reference to the full path /usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/bin/python3.9; we may use /usr/local/bin/python3 instead.

If we open the Get info window of /usr/local/bin/python3, we can see it points to the original location.

img_shadow_soft

Besides symlinks, there is also alias. For instance, when I run which python on my Mac, it returns

which python
python: aliased to /usr/local/bin/python3

As I just mentioned, /usr/local/bin/python3 is a symlink pointing to /usr/local/Cellar/python@3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/bin/python3.9.

Enough has been said. It took me a while to gradually understand these things. I hope you are not confused.

i dosent work

i want to ask if you know other tool for mac
that executive script or file with hot key

Can create a 'quick action' in Automate using an AppleScript to tell Terminal to do something like:

tell application "Terminal" to do script "python myscript.py"

What doesn't work?

I was trying my best to explain where to find python is installed and how to set them up in your environment path. Maybe I did not say clearly enough.

Can you do a simply thing?

  1. Open Script Editor.app
  2. Go to FileOpen Dictionary...
  3. Look for Python.app and take a screenshot of all of them and post here.

It looks like this:

Hey Daniel,

Yes, you can tell the Terminal to run a script with AppleScript – and Keyboard Maestro can run the AppleScript.

See this post:

"Insert text by typing" doesn't work in the Terminal - #4 by ccstone

-Chris

It not Execute the script
It just open the Terminal

The script is this : username =input("What is your name?")
print(username)

Screen Shot 2021-10-26 at 0.32.51

i want to do whet the you suggest
but with all path , not with name of the python file
like this :/Users/guyatia/PycharmProjects/guy6666/main.py

Hey Daniel,

That's no surprise. You apparently didn't read or didn't understand the post I pointed you to.

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2021/10/25 16:52
# dMod: 2021/10/25 16:52
# Appl: Terminal
# Task: Run a Python Script (on disk) in the Active Tab of the Front Window.
#     : Open a new window if active tab is busy.
# Libs: None
# Osax: None 
# Tags: @Applescript, @Terminal, @Python, @Run, @Script
--------------------------------------------------------

set shCmdStr to text 2 thru -2 of "
python3 ~/'test_directory/Python_Test/python_test_02.py'
"

try
   
   tell application "Terminal"
      if (busy of front window) = true then
         
         # Opens a new window if needed.
         do script shCmdStr
         
      else if (busy of front window) = false then
         
         # Runs in the active tab of the front window
         tell front window
            do script shCmdStr in selected tab
         end tell
         
      end if
   end tell
   
on error eMsg number eNum
   tell me to display dialog eMsg & return & return & eNum
end try

--------------------------------------------------------

Your big problem is that you're trying to do something ==interactive== that requires an actual Terminal and not a script-runner mechanism like Keyboard Maestro provides.

Keyboard Maestro cannot (and likely will never be able to) do this:

#!/usr/bin/env python
username =input("What is your name?")
print(username)

On the other hand – Keyboard Maestro can do this:

#!/usr/bin/env python
str_a = ("Displaying string variable")
print (str_a)

Since we're simply outputting a string.

-Chris

i wish i could pay you or somebody
to explained and to show me how to do that

this is so hard from me
obstetrical after obstetrical

Finally its working.
I am using python launcher.