TaskWeaver icon indicating copy to clipboard operation
TaskWeaver copied to clipboard

Importing functions from other directories?

Open twtester2 opened this issue 1 year ago • 1 comments

We are trying to write a TaskWeaver plugin that acts like a wrapper to call functions from our code. Set up TaskWeaver in local dev as follows:

our_code/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
TaskWeaver
    project/
        plugins/
            example_plugin.py
            example_plugin.yaml

Importing from our_code into example_plugin:

from my_code.subpackage1.moduleX import my_function

results in TaskWeaver failing to recognize the plugin. It also spits out an error:

Traceback (most recent call last):

FileNotFoundError: [Errno 2] No such file or directory: 'TaskWeaver\\project\\workspace\\sessions\\20231228-193749-07400689\\cwd\\config\\localhost.yml'


During handling of the above exception, another exception occurred:

SystemExit

Without the import, it calls the plugin fine. Trying relative imports with "from ..my_code.subpackage1.moduleX" does not solve the issue. Is TaskWeaver capable of importing functions from a larger project?

twtester2 avatar Dec 28 '23 19:12 twtester2

Hi, @twtester2 ! If you want to import a custom Python package in your plugin function, you can choose one of the following solutions:

  1. Install your package on your env
  2. Move the our_code under the TaskWeaver dir
  3. Put the absolute path of your own package our_code into PYTHONPATH env var
  4. Dynamically import the parent path of the our_code and TaskWeaver dirs in TaskWeaver/taskweaver/__main__.py For your provided folder structure, the revised code should be below:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))

from .cli import __main__

def main():
    __main__.main()

if __name__ == "__main__":
    main()

Plz note that the last kind of solution depends on your code structure.

zhangxu0307 avatar Dec 29 '23 07:12 zhangxu0307