How does one bypass SyntaxError when parsing code?
Question: I am using openpyxl to read an excel file that will have changing values over
time. The following function will take string inputs from the excel sheets to
make frames for Tkinter.

    
    
    def make_new_frame(strng, frame_location, frame_name, frame_list):
        if not(frame_name in frame_list):
            frame_list.append(frame_name)
            exec("global %s" %(frame_name)) in globals()
            exec("%s = Frame(%s)"%(frame_name, frame_location))
    

.... etc. The code itself is quite long but I think this is enough of a
snapshot to address my problem.

Now this results in the following error while parsing:

> SyntaxError: function 'make_new_frame' uses import * and bare exec, which
> are illegal because it is a nested function

Everything in the code I included parsed and executed just fine several times,
but after I added a few more lines in later versions in this function, it
keeps spitting out the above error before executing the code. The error
references the third line in the function, (which, I repeat, has been cleared
in the past).

I added "in globals()" as recommended in another SO post, so that solution is
not working. There is a solution online
[here](http://python.6.x6.nabble.com/Tutor-executing-dynamic-code-with-exec-
td1681904.html) that uses setattr, which I have no idea how to use to create a
widget without eventually using exec. I would really appreciate if someone
could tell me how to bypass the error while parsing or provide an alternative
means for a dynamically changing set of frame names.

Quick Note:

  * I am aware that setting a variable as global in python is generally warned against, but I am quite certain that it will serve useful for my code

Edit 1: I have no idea why this was downvoted. If I have done something
incorrectly, please let me know what it is so I can avoid doing so in the
future.


Answer: I think this is an [X/Y
problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-
problem). You are asking for help with solution Y instead of asking for help
on problem X.

If your goal is to create an unknown number of Frame objects based on external
data, you can store references to the frame in a list or dictionary without
having to resort to using `exec` and dynamically created variable names.

`exec` is a perfectly fine function, but is one of those things that you
should never use until you fully understand why you should never use it.

Here's how to solve your actual problem without using exec:

    
    
    frames = {}
    def make_new_frame(strng, frame_location, frame_name, frames):
        if not(frame_name in frames):
            frames[frame_name] = Frame(frame_location)
        return frames[frame_name]
    

With that, you now have a dictionary (`frames`) that includes a reference for
every new frame by name. If you had a frame named `"foo"`, for example, you
could configure and pack it like this:

    
    
    frames["foo"].configure(background="red", ...)
    frames["foo"].pack(...)
    

If preserving the order of the frames is important you can use an
[OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict).

