Including 3rd party libraries

It’s a Very Bad Idea™ to install (or ask users to install) 3rd-party libraries in the macOS system Python. Alfred-Workflow makes it easy to include them in your Workflow.

Simply create a lib subdirectory under your Workflow’s root directory and install your dependencies there. You can call the directory whatever you want, but in the following explanation, I’ll assume you used lib.

To install libraries in your dependencies directory, use:

pip install --target=path/to/my/workflow/lib python-lib-name

The path you pass as the --target argument should be the path to the directory under your Workflow’s root directory in which you want to install your libraries. python-lib-name should be the “pip name” (i.e. the name the library has on PyPI) of the library you want to install, e.g. requests or feedparser.

This name is usually, but not always, the same as the name you use with import.

For example, to install Alfred-Workflow, you would run pip install Alfred-Workflow but use import workflow to import it.

An example: You’re in a shell in in the Workflow’s root directory and you’re using lib as the directory for your Python libraries. You want to install requests. You would run:

pip install --target=lib requests

This will install the requests library into the lib subdirectory of the current working directory.

Then you instantiate Workflow with the libraries argument:

from workflow import Workflow

def main(wf):
    import requests  # Imported from ./lib

if __name__ == '__main__':
    wf = Workflow(libraries=['./lib'])

When using this feature you do not need to create an file in the lib subdirectory. Workflow(…, libraries=['./lib']) and creating ./lib/ are effectively equal alternatives.

Instead of using Workflow(…, libraries=['./lib']), you can add an empty file to your lib subdirectory and import the libraries installed therein using:

from lib import requests

instead of simply:

import requests