How can Flask/Python import config file that is one level up higher
Question: I'm writing a Flask web app and I ran into a small problem that really bothers
me. This is my microblog.py file:

    
    
    from flask import Flask
    from flask import render_template, flash, redirect
    from forms import LoginForm
    
    app = Flask(__name__)
    app.config.from_object('config')
    
    @app.route('/')
    @app.route('/index')
    ...
    

And this is my project structure:

    
    
    microblog\
      flask\
        <virtual environment files>
      app\
        static\
        templates\
        __init__.py
        microblog.py
        forms.py
      tmp\
      run.py
      config.py
    

My question is why does the config.py file has to be out of the app folder and
is one level up? I can't really tell myself that it's "magic" so I have to
find the answer but I've failed miserably so far.

My findings suggested that it has something to do with the import system
(python import is really weird) but the thing is, my microblog.app.root_path
is ... (/Users/myname/microblog/app) so there is no way it could have seen the
config file. What's going on?

If you know the answer, please rescue my poor soul from this madness. Thank
you in advance!


Answer: Instead of using a Python file for your config, why not use a format more
suitable for these kind of work. One such candidate is JSON, and you could do
something like this (as a variation on what the [Step 2 of the official
tutorial suggests](http://flask.pocoo.org/docs/tutorial/setup/)):

    
    
    import json
    
    with open('../config.json') as f:
        config = json.load(f)
    
    app.config.update(config)
    

And instead of the `config.py` file you would have a `config.json` file (at
the same location) that might look like

    
    
    {
        "DEBUG": true,
        "SECRET_KEY": "development key"
    }
    

Of course, you don't have to use `json`, there are other configuration file
formats out there ready for you to use, but it's up to you to look up on how
to use that.

