Jump to content

Importing local python module fails in script because Alfred.alfredpreferences/workflows/$alfred_workflow_uid is not part of PATH


Recommended Posts

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 by ryfia
minor edit
Link to comment
  • ryfia changed the title to Importing local python module fails in script because Alfred.alfredpreferences/workflows/$alfred_workflow_uid is not part of PATH

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...