ileonemil Posted June 24, 2021 Share Posted June 24, 2021 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
ileonemil Posted June 24, 2021 Author Share Posted June 24, 2021 22 minutes ago, giovanni said: Hi, thank you. What type of action should I choose so? I see run script and terminal command, and in run script you can choose many languages, should I use python from there ? Link to comment
giovanni Posted June 24, 2021 Share Posted June 24, 2021 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: Link to comment
ileonemil Posted June 25, 2021 Author Share Posted June 25, 2021 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.... Link to comment
giovanni Posted June 25, 2021 Share Posted June 25, 2021 remove the cd command, just python3 (or whatever works in terminal) and the absolute path to your python script Link to comment
ileonemil Posted June 25, 2021 Author Share Posted June 25, 2021 23 minutes ago, giovanni said: remove the cd command, just python3 (or whatever works in terminal) and the absolute path to your python script Still nothing... Link to comment
giovanni Posted June 25, 2021 Share Posted June 25, 2021 does this command work in terminal? what is the expected output? Link to comment
vitor Posted June 25, 2021 Share Posted June 25, 2021 (edited) 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 June 25, 2021 by vitor Link to comment
ileonemil Posted June 26, 2021 Author Share Posted June 26, 2021 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
ileonemil Posted June 26, 2021 Author Share Posted June 26, 2021 On 6/25/2021 at 3:43 PM, giovanni said: does this command work in terminal? what is the expected output? Yes it does and the output is adding some lines in a database in an app I use, using the API of that app Link to comment
deanishe Posted June 27, 2021 Share Posted June 27, 2021 (edited) 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 June 27, 2021 by deanishe Link to comment
ileonemil Posted June 27, 2021 Author Share Posted June 27, 2021 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
deanishe Posted June 27, 2021 Share Posted June 27, 2021 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
ileonemil Posted June 27, 2021 Author Share Posted June 27, 2021 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now