I need to import and run a script from a string (path) which is located in another folder. The input needs to be completely dynamic. The code below works when the file is in the same folder but I can't seem to get it working when the file is located elsewhere.
main.py
path = 'bin\TestScript'
module = __import__(path)
my_class = getattr(module, '__main__')
instance = my_class(3,16)
print(instance)
TestScript.py
def __main__(a,b):
return(a*b)
Get the errror: ImportError: No module named 'bin\\TestScript'
on windows os
bin\TestScriptis not a module name. The correct name would be something likebin.TestScript. Module names != file paths.