This is not a good idea, but this is the implementation of it:
from importlib import import_module # Awesome line! :)
for i in range(1000):
test_include = import_module("Test_include_%s" % i)
model = test_include.aDefinedFunction
Regarding the differences between the provided methods:
__import__
is the low-level interface that handles from bla import blubb
and import bla
statements. It's direct use is according to the docs discouraged nowadays.
importlib.import_module
is a convenience wrapper to __import__
which is preferred. The imported module will be recorded in sys.modules
and thus be cached. If you changed the code during the session and want to use the new version you have to reload it explicitly using imp.reload
.
imp.load_module
is even closer to the internals and will always load the newest version of the module for you, i.e. if it is already loaded load_module
is equivalent to a imp.reload
call on the module. However to use this function you have to provide all 4 arguments, which are basically what imp.find_module
returns.