Jump to content

How can i automate a Python script with a workflow ?


Recommended Posts

Hi! I'm searching a way to run a Python script using a Hotkey with Alfred. The script uses external modules so it isn't written with Py standard library. Would be cool having all working in background, no terminal windows opening.....

Is it possible??

Thanks :)

Link to comment
12 hours ago, giovanni said:

The recommendation is to use a run script object set to shell (bash or zsh), and the launch a python script from there as you would in terminal. this allows you to use a proper code editor for troubleshooting. more details and discussions here:

 

 

I've done all we said but it ins't working....

Schermata 2021-06-25 alle 13.53.49.png

Schermata 2021-06-25 alle 13.54.13.png

Link to comment
16 hours ago, ileonemil said:

The script uses external modules so it isn't written with Py standard library.

 

That’s why it isn’t working. Alfred doesn’t load your shell environment, so it won’t know about PYTHONPATH, or pyenv, or whatever you’re doing in your shell’s startup files to make your packages available. Whatever you’re doing there, you have to redo in Alfred’s Run Script. Think of it like the Terminal of a freshly installed macOS—there’s no configuration or customisation done, so you have to make sure to first load what you need.

 

Open the debugger, set the log output to “All Information”, perfom the failing action, and you’ll see exactly what’s wrong.

Edited by vitor
Link to comment
On 6/25/2021 at 5:10 PM, vitor said:

 

That’s why it isn’t working. Alfred doesn’t load your shell environment, so it won’t know about PYTHONPATH, or pyenv, or whatever you’re doing in your shell’s startup files to make your packages available. Whatever you’re doing there, you have to redo in Alfred’s Run Script. Think of it like the Terminal of a freshly installed macOS—there’s no configuration or customisation done, so you have to make sure to first load what you need.

 

Open the debugger, set the log output to “All Information”, perfom the failing action, and you’ll see exactly what’s wrong.

 

Thank you, is there a way to download Python modules so? I tried to open the directory of the workflow with Terminal, but pip said that the package was already installed....from the debugger I see that's not true so I think that I have to download the module in another place....Where?How?

Link to comment
12 hours ago, ileonemil said:

Thank you, is there a way to download Python modules so?

 

You should put your Python scripts in the workflow directory and install dependencies alongside them using pip’s --target flag, e.g. cd to the workflow folder (or right-click on the workflow in Alfred and choose Open in Terminal), then run /usr/bin/python -m pip install --target . package-name other-package-name

 

12 hours ago, ileonemil said:

from the debugger I see that's not true

 

It sounds like you’ve installed a custom Python (with Homebrew at /usr/local/bin/python3).

 

Alfred doesn’t use your shell environment, so it doesn’t have your custom PATH etc.

 

The command python3 in Alfred always runs /usr/bin/python3. Make sure you’re running /usr/bin/python3 when testing your workflow script in a shell. Similarly, when you have multiple Pythons, you never know which one pip is connected to.

 

It’s a very good habit to run pip as /path/to/python -m pip

 

-m tells Python to run an installed library (it runs the __main__.py file if there is one), and a lot of command-line tools can be run this way. If you always run pip that way, you’ll always install the right version of a library in the right place.

Edited by deanishe
Link to comment
2 hours ago, deanishe said:

 

You should put your Python scripts in the workflow directory and install dependencies alongside them using pip’s --target flag, e.g. cd to the workflow folder (or right-click on the workflow in Alfred and choose Open in Terminal), then run /usr/bin/python -m pip install --target . package-name other-package-name

 

 

It sounds like you’ve installed a custom Python (with Homebrew at /usr/local/bin/python3).

 

Alfred doesn’t use your shell environment, so it doesn’t have your custom PATH etc.

 

The command python3 in Alfred always runs /usr/bin/python3. Make sure you’re running /usr/bin/python3 when testing your workflow script in a shell. Similarly, when you have multiple Pythons, you never know which one pip is connected to.

 

It’s a very good habit to run pip as /path/to/python -m pip

 

-m tells Python to run an installed library (it runs the __main__.py file if there is one), and a lot of command-line tools can be run this way. If you always run pip that way, you’ll always install the right version of a library in the right place.

 

Firstly thank you very much. I finally managed to do that in another way last night. I chose 'run script' and put it in bash, then I wrote this /usr/local/bin/python3 <<'EOF' - "$@" and pasted all the script next to it. That's a very long script but it seems all okay :). Is it right? Thanks anyway for your super helpful tips, will use them for sure. Have a good day!

Link to comment
4 hours ago, ileonemil said:

Is it right?

 

That's a pretty weird way to do it, tbh. If the script runs in your shell, you should generally be able to get it to run in Alfred by specifying the absolute paths and possibly setting a few environment variables.

Link to comment
1 minute ago, deanishe said:

 

That's a pretty weird way to do it, tbh. If the script runs in your shell, you should generally be able to get it to run in Alfred by specifying the absolute paths and possibly setting a few environment variables.

 

Yeah I'll try. I did that because the script uses two apis from different services and have to interact with another script to have some credentials. That way I only put that string before it so that I don't have to change nothing. Thank you very much!!! Have a good day :)

Link to comment

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
×
×
  • Create New...