Reputation: 51
I am trying to use Automator on macOS 10.12 to launch a Python 3 script. The script works just fine when I run it from the terminal with the command: python3 my_script.py
.
Automator has a "Run Shell Script" function that uses the /bin/bash shell. The shell will run scripts with the command: python my_script.py
, but this only seems to work for scripts written in Python 2.7.
My script starts with #!/usr/bin/env python3
, which I thought would direct the shell to the correct python interpreter, but that doesn't seem to be the case.
As a workaround, I can get the script to run if I insert the full path to the python interpreter: /Library/Frameworks/Python.framework/Versions/3.5/bin/python3
, but I see this as suboptimal because the commands might not work if/when I update to Python 3.6.
Is there a better way to direct the /bin/bash shell to run Python3 scripts?
Upvotes: 5
Views: 8198
Reputation: 189908
If python
refers to Python 2 then that*s what you should expect. Use python3
in the command line, or defer to the script itself to define its interpreter.
In some more detail, make sure the file's first line contains a valid shebang (you seem to have this sorted); but the shebang doesn't affect what interpreter will be used if you explicitly say python script.py
. Instead, make the file executable, and run it with ./script.py
.
Actually, you can use env
on the command line, too. env python3 script.py
should work at the prompt, too.
Upvotes: -1
Reputation: 1
You could create ~./bash_profile
file and export the path to python 3 bin folder, it would look like
export PATH=“path-to-python3-bin-folder:$PATH”
Every time that you enter Terminal, type source ~./bash_profile
and now you can call python3 as a function in terminal to call your code.
On the other hand, I would recommend you to download Anaconda, as you can create virtual environments easily with the version of python that you’d like.
Upvotes: 0
Reputation: 745
You can install Python 3 via Homebrew with brew install python3
and use #!/usr/local/bin/python3
as your shebang.
Not a perfect solution but still better than using the full path of the interpreter.
Upvotes: 1
Reputation: 3426
Since you have the shebang line, you can do ./my_script.py
and it should run with Python 3.
Upvotes: 2