ryfia Posted January 16, 2023 Share Posted January 16, 2023 (edited) I have a workflow script (set up with Language: /usr/bin/python3 and with input as argv): import sys from commands import OptionalArgsCommand cmd = OptionalArgsCommand('https://redacted.net', 'https://redacted.net/search?q={input}') sys.stdout.write(cmd.get_url(sys.argv[1:])) And in the workflows folder (/Users/$RedactedUser/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.$RedactedWorkflowId) I have a locally created python module (commands.py): class OptionalArgsCommand(object): """A command that encodes arguments (if provided) into the string""" def __init__(self, no_args_url, existing_args_url): self.no_args_url = no_args_url self.existing_args_url = existing_args_url def get_url(self, args): if not len(args): return self.no_args_url return self.existing_args_url.format(input=''.join(args)) The reason for this helper module file is to contain helper utils called in the one-off scripts. The problem is, when running the workflow it complains `ModuleNotFoundError: No module named 'commands'`. I traced this to an issue with the PATH environment variable, where the path doesn't include the ./Users/$RedactedUser/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.$RedactedWorkflowId folder, and so it can't find the `commands` module to import. What is the correct recommended way to import a local python module under the combination of Language: /usr/bin/python3 and with input as argv? I'm currently hacking it by putting this at the top of all my scripts so they can access my local commands.py module. import os sys.path.append(os.environ["alfred_preferences"] + "/workflows/" + os.environ["alfred_workflow_uid"]) This translates to a path like "/Users/$user/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows/user.workflow.37519182-2ECC-43A1-BE8E-5B6C6BB541FZ" getting added to the PATH, allowing the commands.py file that I bundle with the workflow to be available for import. But that seems super hacky. Is there a better way to import the local python module that's shipping with my workflow? Any help would be appreciated! For reference my full path (output of sys.path in python file) in this circumstance is: ['/Users/$RedactedUser/Library/Caches/com.runningwithcrayons.Alfred/Workflow Scripts', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python39.zip', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages'] My system: Macbook Pro 2021, Ventura 13.1 Alfred version: Alfred 5.0.6 [2110] Edited January 17, 2023 by ryfia minor edit Link to comment
vitor Posted January 17, 2023 Share Posted January 17, 2023 Welcome @ryfia, To include a package with the workflow, use the --target option and set PYTHONPATH. StackOverflow has other options. 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