Dr. Peter Posted April 1, 2023 Share Posted April 1, 2023 First, there have been a couple threads here and around the internet that haven't answered my problem or have danced around it to no avail. I have Alfred 5.06 (the latest at the time of this post) with Python 3.9 installed on my MacBook with Ventura 13.3. I am writing a python3 script to post an html request. I have installed the responses module for both python 3.10 and 3.9 that happened to be installed on my system and verified the installations, even uninstalling and reinstalling twice. I can run my script through the python interpreter directly and everything works completely, but when I try to run it in an Alfred Workflow (after having selected the /usr/bin/python3 script option), I get an error from the line: ... import responses ... Error: Traceback (most recent call last): File "/Users/$MyNameHere/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts/$WorkflowLongGibberishName", line 7, in <module> import requests ModuleNotFoundError: No module named 'requests' I've also added the shebang at the top of the script to try to force the script to use the other installation: #!/Users/$MyNameHere/opt/anaconda3/bin/python3 I've tried a bunch of other things including adding the /Users/$MyNameHere/opt/anaconda3/lib/python3.9/site-packages directory to the PATH. Can someone please help me fix this? I've spent to much time already on something that was supposed to be done days ago. Thanks, Peter Link to comment
Dr. Peter Posted April 1, 2023 Author Share Posted April 1, 2023 I found a workaround. I just copied and pasted the exact script, with no changes at all, into an external file and ran it as an external script, and it worked completely. Perhaps Alfred is not respecting the shebang trying to try to force the script to use the correct python3 installation: #!/Users/$MyNameHere/opt/anaconda3/bin/python3? Peter Link to comment
vitor Posted April 1, 2023 Share Posted April 1, 2023 (edited) You can’t set an interpreter than have a shebang and have it work. The point of a shebang is that it chooses the interpreter when none is specified (i.e. when running as an External Script or calling with ./myscript argument1 argument2). If you specify an interpreter to run some code, the shebang is ignored. So if you say /usr/bin/python3 ./myscript it will be run with /usr/bin/python3 no matter what, regardless if the shebang points to another installation of python or an entirely different language. This is not specific to Alfred, it’s how it works everywhere. See also understanding the scripting environment. Edited April 1, 2023 by vitor Link to comment
pixelgeek Posted April 2, 2023 Share Posted April 2, 2023 To add on to what @vitor is saying. The python environment is set in the Run Script object from the dropdown. It won't respect any environment change that you set inside the script. When you set the script to be external it runs the script from outside Alfred. Alfred is no longer controlling the environment that the script runs under and so you directive to use a specific environment will then work. 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