Using exec vs import in python
Question: In django it's very common to use a `local_settings.py` file to supplement
`settings.py` so that each machine can have different settings. Usually people
do something like this.

    
    
    try:
        from local_settings import *
    except ImportError:
        print "No local settings found!"
    

But the settings file usually contains large lists such as `INSTALLED_APPS`.
If I want to add an app, I'd rather not copy the entire list into
`local_settings.py` and modify it (makes for less readable code, and updating
`settings.py` no longer affects any machine that re-writes `INSTALLED_APPS`).
So I figured I could do something like this:

    
    
    try:
        f = open('local_settings.py','r')
        exec f.read()
    except IOError:
        print "No local settings found!"
    

Now `local_settings.py` no longer has to rewrite the entire `INSTALLED_APPS`
variable:

    
    
    INSTALLED_APPS.append('debug_toolbar')
    

I was under the impression that anytime you're using `exec` or `eval` you're
probably doing something wrong. So my question is, is there anything wrong
with this and is there a better way to do it?


Answer: There is a much better way: import `INSTALLED_APPS` in `local_settings.py`,
copy it, then add to the copy:

    
    
    import settings
    
    INSTALLED_APPS = settings.INSTALLED_APPS[:] + ['debug_toolbar']
    

