As discussed here, we can dynamically import a module using string variable.
import importlib
importlib.import_module('os.path')
My question is how to import *
from string variable?
Some thing like this not working for now
importlib.import_module('os.path.*')
You can do the following trick:
Be warned that makes all names in the module available locally, so it is slightly different than
*
because it doesn't start with__all__
so for e.g. it will also override__name__
,__package__
,__loader__
,__doc__
.Update:
Here is a more precise and safer version as @mata pointed out in comments:
Special thanks to Nam G VU for helping to make the answer more complete.