Lets say I have some function that gets called during runtime. In this function I want to import a module given a name as argument.
This is the basic idea. Essentially I have a directory full of scripts. I want to import the given script by name so that it can dynamically be used in my code without changing any variable names etc.
def some_func(PARAMATER):
eval("exec('import Some." + PARAMATER + ".module as myModule')")
module.test()
The error I get is:
NameError: name 'myModule' is not defined
I tested this code several time with different approaches. Whenever it is inside of a function block, I get an error. Whenever it is not, it works perfectly.
If there are any better ideas of dynamically loading a module and being able to change it during runtime, I would happily use another approach.