TaskWeaver
TaskWeaver copied to clipboard
Importing functions from other directories?
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?
Hi, @twtester2 ! If you want to import a custom Python package in your plugin function, you can choose one of the following solutions:
- Install your package on your env
- Move the
our_code
under theTaskWeaver
dir - Put the absolute path of your own package
our_code
intoPYTHONPATH
env var - Dynamically import the parent path of the
our_code
andTaskWeaver
dirs inTaskWeaver/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.