I have two files, in the first one where functions are defined named 'functions.py' and the second one where I can call those functions named 'test.py' and I am executing some code with exec from 'test.py' and asking it's asking for the variable initialized in 'for' loop while being 'for' loop through exec but I am getting error
NameError: name 'x' is not defined
Details:
In 'functions.py' I describe a function named 'forloop' like this:
def forloop(current, maximum, increment, code):
frame = inspect.currentframe().f_back
for x in range(current, maximum, increment):
exec(str(code), frame.f_globals, frame.f_locals)
And in 'test.py' I am calling this function
from functions import *
forloop(1,5,1,"""
echo(x)
""")
Now I am executing it with another exec in another file but, I am pretty sure that problem lies in here.
I think I should be able to access 'x' from this exec but, I can't, it gives me an error saying
NameError: name 'x' is not defined
Please tell me why 'x' isn't defined and how can I get 'x' from that file.