How to use Automator to run a Python 3 Code

I wrote a python code (using python 3) and I'm trying to use Automator's Run Shell Script to run my code. I have been doing research to try to understand how to make this happen but I don't understand which type of shell option to choose and what to type under that.

I tried something but automator automatically ran the program on python 2, but my program needs python 3 in order to work properly

Thanks!

Posted on May 15, 2018 5:53 AM

Reply
Question marked as Top-ranking reply

Posted on May 15, 2018 6:23 AM

You can leave the Run Shell Script shell set to Bash, because using the Python shell would seek Python2. The very first line in your Python code should be:


#!/usr/local/bin/python3


and it will need to understand how to access your Python3 library modules.


If your Python code is getting command-line arguments that you pass in from a prior Automator action, then you need Pass input: as arguments.

8 replies
Question marked as Top-ranking reply

May 15, 2018 6:23 AM in response to julianhtc

You can leave the Run Shell Script shell set to Bash, because using the Python shell would seek Python2. The very first line in your Python code should be:


#!/usr/local/bin/python3


and it will need to understand how to access your Python3 library modules.


If your Python code is getting command-line arguments that you pass in from a prior Automator action, then you need Pass input: as arguments.

May 17, 2018 8:26 AM in response to julianhtc

My example of returning the Python library path served two purposes. It shows how to run Python3 in Bash without it being contained in a Bash function, and it shows how to determine the PYTHONPATH to the Python3 library (boxed red content).


As long as there is no syntax, or module naming issue, Python3 is also very happy to dip into the System Python 2.7.10 modules, and you don't want that.


If the code example runs for you and returns the path to your Python3 library, then your Python3 is finding its library, and you just replace the Python code that I used with your own.


If the code that I provided bombs because it could not find your Python3 library, or it shows a path to the System Python 2.7 library path, then you need to determine where your package manager installed it, and then do the following prior to the Python3 HERE script line:


export PYTHONPATH="/usr/local/lib"


or whatever the appropriate path.

May 19, 2018 10:06 AM in response to julianhtc

If your first action is that screen shot saved to a specific folder, then run Automator interactively, with the Results expanded on that action, and see if it is passing the full path to the image on to your Run Shell Script.


If the full path to the captured image is being passed to your Run Shell Script, and you want Python to access that image, then you need the Pass input: set to as arguments, and you need to get that path from your sys.argv[1] which is the contents of "$@" in the shell.


/usr/local/bin/python3 <<'EOF' - "$@"

import sys


if not len(sys.argv) == 2:

sys.exit('wrong argument count')

set img_file to sys.argv[1]

May 19, 2018 9:27 AM in response to VikingOSX

Your example code worked for me! I tried using my own code and I think it's working, but the problem is in my code I try to access a specific image file saved where my code is. When I try running Automator, it says it can't find that file. How do I tell Automator where to look?


This could help simplify what I'm trying to do: The first step is for automator to take a screenshot and then save it as 'testImage.jpg' in a specific folder. I then want to use that same image in my code.

May 15, 2018 6:47 AM in response to julianhtc

Ignore my initial post. I just tested the following run shell script using the binary installation of Python 3.6.5 from Python.org. It appears to know where its library modules are located. How did you install your Python3?


The following simply encapsulates the Python code within a Bash function, and then runs that function — in this case without any command-line arguments passed into the action.


User uploaded file

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to use Automator to run a Python 3 Code

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.