When I run the following sample:
def a():
exec('import math')
b()
def b():
print math.cos(90)
a()
I get the following error:
NameError: global name ‘math’ is not defined
What I am trying to do is to dynamically load some modules from within the a() function
and use them in function b()
I want it to be as seamless as possible for the b()’s point of view. That means, I don’t want to load the module with _ _ import _ _ in a() and pass a reference to the b() function, in fact it is mandatory that the b()’s function signature remains just this: b()
is there any way to do this guys?
thanks!
Upon comments on the post: if want to load modules runtime, load where you need it:
Answering to your question: