Jump to content

ryfia

Member
  • Posts

    1
  • Joined

  • Last visited

ryfia's Achievements

Helping Hand

Helping Hand (3/5)

0

Reputation

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