简体   繁体   中英

Where does Flask look to find config files when using flask.config.from_object()?

I've been trying to solve this for a couple days now. I'm running flask using the application setup as described in the tutorial . Below is are the packages installed in the virtual environment.

pip3 freeze

click==7.1.2 Flask==1.1.2 itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 pkg-resources==0.0.0 python-dotenv==0.15.0 Werkzeug==1.0.1

I have the following in a.env file:

FLASK_APP=myapp

just so I can do flask run . My directory structure looks like:

目录结构

This is all contained within a directory called 'proj'

init .py

import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object('conf.DevConfig')
    app.config.from_mapping(DATABASE=os.path.join(app.instance_path, 'tester.sqlite'))

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    return app

in proj I run flask run and get the following:

werkzeug.utils.ImportStringError: import_string() failed for 'conf.DevConfig'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Debugged import:

- 'conf' not found.

Original exception:

ModuleNotFoundError: No module named 'conf'

Notice that there is a conf.py in proj/instance/

conf.py contains

class Config(object):
   DATABASE = 'tester.sqlite'

class DevConfig(Config):
   DEBUG = True

class ProdConfig(Config):
   DEBUG = False

Oddly enough, if I put conf.py in proj/ then the application loads just fine. I could have swore I read that Flask will search proj/instance by default for files. Why does it find it when I move it one level back from instance. For that matter can.env files be stored in instance and will flask auto find them? To me it seems like instance_relative_config=True isn't doing what it should be doing. What effect does calling flask run have if it is run in proj/ vs proj/myapp

you can consider this as (Not 100% sure), when you do flask run then create_app function is called and runned.

it is simply as adding a run_app.py file in proj outside of app which import create_app function as as run it

so run_app.py

 from app import create_app
 app = create_app()
 if __name__=='__main__':
      app.run()

this will run the server ( as you are doing now) export FLASK_ENV=app or export FLASK_ENV=run_app.py are same

now as you have added config file in conf.py and it is working fine for you when that file is in proj folder not in instance folder. This is happening because in app/__init__.py file you have define the location of the config object as conf.DevConfig , so python is looking for this object in proj folder either.

since you have define the config object in instance/conf.py file, so python is not able to find it and giving you this error.

To solve This, you need to provide the location of the object in app.config.from_object('conf.DevConfig')

so you can do this in two way,

1st. provide full conf object path from the proj folder like

 app.config.from_object('instance.conf.DevConfig')

2nd. import the instance in the app/__init__.py file and then provide the config object

eg. app/__init__.py

"""      

import other packages

"""
from instance import conf


def create_app(test_config=None):
    """
    app config thing here
    """
    app.config.from_object(conf.DevConfig)
    """
    config app more
    """
    return app

Note in method one i am providng object as a string and in 2 as a class method

app.config.from_object() will search from where flask run is executed. In this case from the /proj directory

app.config.from_pyfile() will search at least in instance when instance_relative_config is set to true

As mentioned by sahasrara62 : the devConfig class can be found in conf.py by using 'instance.conf.DevConfig'

I didn't notice any evidence that flask run add a run_app.py file and executes it, but it does seem that flask run starts off in the directory it is run from

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM