There are ways to load a module with other file extensions, however, if it is your own custom made toolbox, you should rather refactor your toolbox and keep the toolbox's functionality in a separate module which is then used by your toolbox and your external script. Paul H's comment points to a good example.
However, if you are not able to refactor the toolbox, then you could use the package importlib
. Here is an example based on another SO answer:
For this example, consider that your toolbox.pyt
has a function say_hello()
.
from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader
spec = spec_from_loader("toolbox", SourceFileLoader("toolbox", r"/path/toolbox.pyt"))
toolbox = module_from_spec(spec)
spec.loader.exec_module(toolbox)
toolbox.say_hello()
In addition, adding sys.modules['toolbox'] = toolbox
allows you to import toolbox
in other Python files after above code run.
Note: IMHO, if you have a chance to refactor the toolbox, a refactoring should be preferred.