Importing from a Module Whose Name Is Determined at Runtime
Credit: Jürgen Hermann
Problem
You
need to import a name from a module, such as from
module import
name, but
module and name
are runtime-computed expressions. This need often arises, for example
when you want to support user-written plug-ins.
Solution
The _ _import_ _ built-in function allows this:
def importName(modulename, name):
""" Import a named object from a module in the context of this function.
"""
try:
module = _ _import_ _(modulename, globals(), locals( ), [name])
except ImportError:
return None
return vars(module)[name]Discussion
This recipe’s function lets you perform the
equivalent of from
module import
name, in which either or both
module and name
are dynamic values (i.e., expressions or variables) rather than
constant strings. For example, this can be used to implement a
plug-in mechanism to extend an application with external modules that
adhere to a common interface. Some programmers’
instinctive reaction to this task would be to use
exec, but this instinct would be a pretty bad one.
The exec statement is a last-ditch
measure, to be used only when nothing else is available (which is
basically never). It’s just too easy to have horrid
bugs and/or security weaknesses where exec is
used. In almost all cases, there are better ways. This recipe shows
one such way for an important problem.
For example, suppose you have in the
MyApp/extensions/spam.py file:
class Handler: def handleSomething(self): print "spam!" ...