importing a module when the module name is in a variable
You want the built in __import__
function
new_module = __import__(modulename)
importlib is probably the way to go. The documentation on it is here. It's generally preferred over __import__
for most uses.
In your case, you would use:
import importlib
module = importlib.import_module(module_name, package=None)