How can Flask/Python import config file that is one level up higher How can Flask/Python import config file that is one level up higher flask flask

How can Flask/Python import config file that is one level up higher


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):

import jsonwith 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.


Based on that Flask tutorial, run.py is what you're actually running when you start the app, and run.py is in the same directory as config.py. When you run python from the command line, the current directory is generally on your module path, so if you run run.py from that directory, then config is available on your path (as an absolute import)

I just glanced at the deployment part of the tutorial but it's possibly counting on the microblog directory being the working directory.