I have a bunch of modules that I need to import. For reasons I not permitted to explain the module names must be stored as strings in a list. In other words I need to do the following:
modules_to_import = ['module1', 'module2', 'module3']
import modules_to_import
Does anybody know if I can to that in python
?
Use
importlib.import_module
:If you want to access the modules as global variables, you'll have to do some hacky stuff, such as assigning to
globals()
: