You can use __import__
if you have a list of strings that represent modules, but it's probably cleaner if you follow the hint in the documentation and use importlib.import_module
directly:
import importlib
requirements = [lib1, lib2, lib3, lib4, lib5]
imported_libs = {lib: importlib.import_module(lib) for lib in requirements}
You don't have the imported libraries as variables available this way but you could access them through the imported_libs
dictionary:
>>> requirements = ['sys', 'itertools', 'collections', 'pickle']
>>> imported_libs = {lib: importlib.import_module(lib) for lib in requirements}
>>> imported_libs
{'collections': <module 'collections' from 'lib\\collections\\__init__.py'>,
'itertools': <module 'itertools' (built-in)>,
'pickle': <module 'pickle' from 'lib\\pickle.py'>,
'sys': <module 'sys' (built-in)>}
>>> imported_libs['sys'].hexversion
50660592
You could also update your globals
and then use them like they were imported "normally":
>>> globals().update(imported_libs)
>>> sys
<module 'sys' (built-in)>