I'm trying to import a function from another module; however, I can't use import
because the module's name needs looking up in a list.
If I try to call the imported function ExampleFunc
normally I get:
NameError: global name 'ExampleFunc' is not defined
However; if I explicitly tell python to look in locals, it finds it.
File module.py
def ExampleFunc(x):
print x
File code.py
def imprt_frm(num,nam,scope):
for key, value in __import__(num,scope).__dict__.items():
if key==nam:
scope[key]=value
def imprt_nam(nam,scope):
imprt_frm("module",nam,scope)
def MainFunc(ary):
imprt_nam("ExampleFunc",locals())
#return ExampleFunc(ary) #fails
return locals()["ExampleFunc"](ary) #works
MainFunc("some input")
The locals()
dictionary is but a reflection of the actual locals array. You cannot add new names to the locals through it, nor can you alter existing locals.
It is a dictionary created on demand from the actual frame locals, and is one-way only. From the locals()
function documentation:
Note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.
Function locals are highly optimised and determined at compile time, Python builds on not being able to alter the known locals dynamically at runtime.
Rather than try and stuff into locals directly, you can return the one object from the dynamic import. Use the importlib
module rather than __import__
here:
import importlib
def import_frm(module_name, name):
module = importlib.import_module(module_name)
return getattr(module, name)
then just assign to a local name:
def MainFunc(ary):
ExampleFunc = import_from('module' , 'ExampleFunc')
ExampleFunc(ary)