The "exec" function seems to literally execute strings as code in a Python shell:

>>> exec("a=2")
>>> a

However, when I tried this function from within a tkinter form, the variables were no longer accessible via their original names:

a=1 #initialize the variable "a"
exec(str(CodeTextBox.get())) # I entered "a=2" into CodeTextBox, then activated "exec" with a run button placed on the same form.
ResultTextBox.insert(0, a) #This returned "1" instead of the expected "2".

I tried entering "print(1)" (I'm using Python 3) and a "1" was printed, so it is running the "exec(...)" code properly.

So here's my question:

Is there a way to find out where a variable of a given name is stored in compiled Python code? Maybe a dictionary mapping the original variable names to numbered objects?

I would like to eventually make a way to edit a program while it is running (the only place I've encountered this was in an audio/video graphical programming environment called "Pure Data").

One way to approximate this would be to store all the variables (plus the current position) in memory. Then you could stop execution, edit the code (for example: change the definition of a key function), and restart it in the same state.

A standard way to save states between executions is to store persistent objects on disk. Look if the pickle module can help you.