pixelgeek Posted June 27, 2023 Share Posted June 27, 2023 (edited) I am trying to build a python script that uses the frontmatter library to read some YAML data from files. The library is installed. It shows up in `pip list` and pip will also show the details of the library. Name: python-frontmatter Version: 1.0.0 Summary: Parse and manage posts with YAML (or other) frontmatter Home-page: https://github.com/eyeseast/python-frontmatter Author: Chris Amico Author-email: eyeseast@gmail.com License: MIT Location: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages Requires: PyYAML Required-by: I tested the code in PyCharm as well before I ported it to Alfred and it ran correctly there. When I drop the code into Alfred I get the following error. I am using the /usr/bin/python3 option Code 1: Traceback (most recent call last): File "/Users/Zac/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/B98FC667-F18E-4119-80F5-A521C67631EC", line 4, in <module> import frontmatter ModuleNotFoundError: No module named 'frontmatter' I moved the script to run externally and that works just fine. When I use `which python3` it shows the correct path. How is Alfred not finding the library if it is installed at the system path for Python? I also do not have Homebrew python installed Edited June 27, 2023 by pixelgeek Link to comment
vitor Posted June 28, 2023 Share Posted June 28, 2023 (edited) See Understanding the Scripting Environment for details on the difference between running scripts in a Terminal and outside of it (including Alfred). It may give you the clue you seek. On 6/27/2023 at 7:02 PM, pixelgeek said: How is Alfred not finding the library if it is installed at the system path for Python? In general you should avoid installing there to avoid issues. The more pristine you keep the system folders, the better. To include a Python package with a workflow, use the --target option and set PYTHONPATH. StackOverflow has other options. On 6/27/2023 at 7:02 PM, pixelgeek said: I moved the script to run externally Do you mean you set the language to External Script, or are you running it from a shell? If the former, what’s the shebang in the script? If the latter, what’s the command you’re using? Is the language set to /bin/zsh? If so, do you perchance have a .zshenv file in your home? If calling from Zsh, could you temporarily update to the 5.1.2 pre-release and see what happens? Edited June 28, 2023 by vitor Link to comment
pixelgeek Posted June 29, 2023 Author Share Posted June 29, 2023 3 hours ago, vitor said: To include a Python package with a workflow, use the --target option and set PYTHONPATH. StackOverflow has other options. That may have been good advice some time ago but no-one suggests that you do that anymore. That link is to a 13 year old article and modifying the PYTHONPATH is a fairly big no-no now. When I develop python apps I use a .venv but I can't do that for something that runs in Alfred. 3 hours ago, vitor said: Do you mean you set the language to External Script, or are you running it from a shell? It is set to External Script. It runs correctly when I do that but if I put the same code in the Script Filter I get the library error. The problem is solved in that I have code that runs but I don't understand how Alfred appears to be accessing a different version of Python or one that is in a different location. Link to comment
pixelgeek Posted June 29, 2023 Author Share Posted June 29, 2023 So I did some poking around. It appears that Alfred's version of Python is coming from `/Library/Developer/CommandLineTools/usr/bin` and is running Python 3.9 Is there any way to get it to run from where I have Python installed or is that not practical? Link to comment
vitor Posted June 29, 2023 Share Posted June 29, 2023 5 hours ago, pixelgeek said: That link is to a 13 year old article and modifying the PYTHONPATH is a fairly big no-no now. Note that you’re not modifying it permanently, only for that session/environment. The reason venv is recommended is that it creates a temporary virtual environment without messing up the rest of your shell configuration. That is not a concern with Alfred because every workflow runs in a pristine environment, as if you had started fresh all your shell configuration. Bundling packages with the workflow gives you that same separation, ensuring packages from one workflow do not interfere with another and that non technical users don’t need to install things themselves. Workflows in the Gallery follow the bundling approach for that reason. 5 hours ago, pixelgeek said: Is there any way to get it to run from where I have Python installed or is that not practical? Alfred uses /usr/bin/python3 because it is the common denominator all users have access to that all users have. That said, calling a Python runtime from another location is trivial. But it can’t be accepted to the Gallery in that case because it will be a setup most users don’t have. Note the first approach in that link is basically what you’ve discovered above in your working solution. pixelgeek 1 Link to comment
pixelgeek Posted June 29, 2023 Author Share Posted June 29, 2023 3 hours ago, vitor said: That is not a concern with Alfred because every workflow runs in a pristine environment, as if you had started fresh all your shell configuration. I see the difference now. Thanks for clarifying that. I'll play around with the Workflow and see if I can get it working the way you suggested. 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