Jump to content

Calling Python 3 (and other non-standard runtimes) from Alfred


Recommended Posts

When configuring a Run Script or Script Filter, Alfred provides a Language dropdown listing the runtimes which have historically been included with macOS. But what if you want to run a script from a language which, despite not shipping by default, you have installed on your system? Be it Python 3, Node.js, Lua, or something else, it’s dead-simple to call them. Either:

  • Save your script with a proper shebang (examples: #!/usr/bin/python3; #!/opt/homebrew/bin/lua) and call it with External Script as the Language.
  • Use /bin/zsh (or /bin/bash) as the Language and use the full path to the runtime when calling your script (examples: /usr/bin/python3 MY_SCRIPT.py; /opt/homebrew/bin/lua MY_SCRIPT.lua).

The first executes (marginally) faster but the second allows you to send preset arguments to your script.

 

A variant of the second solution makes your Workflow more suitable for sharing. Expand the PATH variable and call your runtime with a relative path:

 

export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}"
python3 MY_SCRIPT.py

 

export PATH="/opt/homebrew/bin:/usr/local/bin:${PATH}"
lua MY_SCRIPT.lua

 

To learn more about the scripting environment, check the knowledge base article.

Edited by vitor
Link to comment
Share on other sites

I have several other options as well.

 

1) Look for the binaries directly

#!/bin/bash
# ----------------------------------------------------------------------
# Find Python Binary
# ----------------------------------------------------------------------
if [ -e /usr/bin/python ]; then 
    python_binary="/usr/bin/python"
elif [ -e /opt/homebrew/bin/python ]; then
    python_binary="/opt/homebrew/bin/python"
# Add any otter locations you can think of as well, or python3 locations
else
    echo "Cannot find Python binary"
    exit 1
fi

# ----------------------------------------------------------------------
# Execute Code
# ----------------------------------------------------------------------
$python_binary script.py

 

2) Use /usr/bin/env to find the command

/usr/bin/env python script.py
# OR
/usr/bin/env python3 script.py

 

3) This one works for scripts that are set to use Python instead of Bash.  Copy the code, set it to bash, then paste using the following pattern.

echo "print('My entire python script goes here!  Make sure to escape any double quotes!')"| python #or python3 or the full path to the binary

 

4) For actually coding in Python, make sure you have a config.py that contains a PYTHON_BINARY variable and use the logic from solution 1 to find it, then import it anywhere you're calling python (usually in your workflow external runner).  I can't tell you how many times I've updated a workflow just to find out theres a workflow.py that references /usr/bin/python still that I had to go fix.

Link to comment
Share on other sites

12 hours ago, CandiceJoy said:
/usr/bin/env python script.py
# OR
/usr/bin/env python3 script.py


Note you never need /usr/bin/env in this situation. Removing it makes the line work the same.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...