Python how to import custom function to another script (problem in Task Scheduler)

746 views Asked by At

I have to import custom functions from script A to script B and execute them in script B.

Now it looks like this:

Script_B.py

import os
import sys
from A import function_aa
from A import function_ab
from A import function_ac


def main():
    function_aa()
    function_ab()
    function_ac()

if __name__ == "__main__":
    main()


If I run it in the IDE it is ok, but Windows Task Scheduler doesn't recognize these custom functions and throws an error ModuleNotFoundError: No module named 'A'

Ok I tried to include a path to A like this:

import importlib
import sys

MODULE_PATH = 'C://foo//bar//__init__.py' # A.py is in bar folder and contains A.py and __init__.py
MODULE_NAME = "A"
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)

Then I have this error ImportError: cannot import name 'function_aa' from 'A'

Is there any workaround this Task Scheduler problem with importing your own function?

0

There are 0 answers