Dynamic Import with Python
Rewriting my application on Django,
I needed to import classes without knowing their names first
(just reading them out of a directory).
Obviously something like this:
|
|
will just try to import "module_name", and doesn't work the way it's suppose to.
So, what did I do? I solved it by using the
__import__
function!__import__ lets you import modules with parameters given, for instance:
|
|
After implementing this, I found out that there's a better, more convenient wrapper for the __import__ function,
the code for which would look like this:
|
|
Both are valid though (because importlib is only a wrapper)
Posted in
Technology