简体   繁体   English

未使用FLASK_APP和App Factory加载烧瓶配置

[英]Flask config not being loaded using FLASK_APP and App Factory

I have an application set up, trying to configure it with a .env file, using python-dotenv to load it and a Config class to get the variables. 我设置了一个应用程序,尝试使用.env文件配置它,使用python-dotenv加载它,并使用Config类获取变量。 If I set the FLASK_APP, for example: export FLASK_APP=pg.py, and do flask run, the app runs, but the config isn't loaded. 如果我设置了FLASK_APP,例如:export FLASK_APP = pg.py,并执行flask运行,则该应用程序将运行,但未加载配置。 I know the environmental variables are being loaded from my .env file, and after lots of tinkering, I realized that the app is being created with no context (I think), but I'm not sure how to get it do so. 我知道环境变量是从我的.env文件中加载的,经过大量修改后,我意识到该应用程序是在没有上下文的情况下创建的(我认为),但是我不确定如何做到这一点。 If I add app.run() to the end of pg.py, it works, but I know that is not what I'm supposed to do. 如果我在pg.py的末尾添加app.run(),它可以工作,但是我知道那不是我应该做的。 I've read this page: http://flask.pocoo.org/docs/0.12/cli/ several times, and I can't quite follow it to get what I'm after. 我已经阅读了以下页面: http : //flask.pocoo.org/docs/0.12/cli/多次,但我不太了解它以获取所追求的东西。 I tried export FLASK_CONFIG=development as my last attempt, to see if that would work correctly, and it did not. 我尝试将FLASK_CONFIG = development作为最后一次尝试,以查看是否可以正常工作,但没有成功。 So my question is, how do I use flask run, and get it to run my app correctly as opposed to running 'python pg.py' 所以我的问题是,我如何使用flask run,并使其正确运行我的应用程序,而不是运行“ python pg.py”

Edit: I notice everything works properly, except the configuration. 编辑:我注意到除了配置之外,其他所有东西都可以正常工作。 Not sure what I am missing. 不知道我在想什么。 I feel like, based on what I've read, that FLASK_CONFIG=development should work. 根据我的阅读,我觉得FLASK_CONFIG = development应该可以工作。 It's almost as if the app gets created with 'app = Flask( name )', skips the config loading, and goes to the Blueprint registration. 几乎就好像该应用是使用'app = Flask( name )'创建的,跳过了配置加载,并转到了Blueprint注册。 Why? 为什么? I know I can export all my sensitive information before running, and do it that way, but now I am stuck on trying to figure out a problem, and won't be able to let it go until I understand :/ 我知道我可以在运行之前导出所有敏感信息,并以这种方式进行处理,但是现在我一直想找出问题所在,直到我理解了://

Edit 2: Actually, the context is created properly and the configuration is applied. 编辑2:实际上,已正确创建了上下文并应用了配置。 It just doesn't read from the environment. 它只是不从环境中读取。 DEBUG=True doesn't work, but other configurations do. DEBUG = True不起作用,但其他配置起作用。 Perhaps the app instance is created before the environment is loaded with variables from .env. 可能在环境加载.env中的变量之前创建了应用实例。 I will leave this here, and update with a solution when I find it. 我将把它留在这里,并在找到解决方案时进行更新。

app/__init__.py 应用程序/ __ init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import config

db = SQLAlchemy()


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    db.init_app(app)

    from app.main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

config.py config.py

import os

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'environment not set'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DB_URI')

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True


config = {
    'development': DevelopmentConfig
}

pg.py pg.py

from app import db, create_app
from flask_migrate import Migrate
import os
from dotenv import find_dotenv, load_dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
if os.path.exists(dotenv_path):
    load_dotenv(dotenv_path)
    print("secret key:", os.environ.get('SECRET_KEY'))
else:
    print('no env found')

app = create_app(os.getenv('FLASK_CONFIG'))
migrate = Migrate(app, db)

A kind soul over at IRC #pocoo helped me see the light. IRC #pocoo的善良灵魂帮助我看到了光明。 The answer is so simple. 答案很简单。 The .env needs to be loaded before importing create_app, which also imports the configuration file. 在导入create_app之前,需要先加载.env,后者还会导入配置文件。

import os
from dotenv import find_dotenv, load_dotenv

dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
load_dotenv(dotenv_path)

from app import db, create_app
from flask_migrate import Migrate
app = create_app(os.getenv('FLASK_CONFIG'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FLASK_APP for Quart - FLASK_APP for Quart 为什么FLASK_APP无法找到我的工厂功能? - Why can't FLASK_APP find my factory function? 错误:无法在模块“app”中找到 Flask 应用程序或工厂。 使用 'FLASK_APP=app:name' 指定一个 - Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one 在模块“app”中找不到 Flask 应用程序或工厂。 使用 'FLASK_APP=app:name' 指定一个 - Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one 在创建应用程序时如何使用 Flask 应用程序配置(使用应用程序工厂)? - How to use Flask app config while the app is being created (using an app factory)? 尽管指定的 FLASK_APP 文件被重命名,但 FLASK RUN 仍然有效 - FLASK RUN still working despite the FLASK_APP file specified being renamed Docker 运行错误 - 无法在模块“app”中找到 Flask 应用程序或工厂。 使用 "FLASK_APP=app:name 指定一个 - Docker run error - Failed to find Flask application or factory in module "app". Use "FLASK_APP=app:name to specify one 找不到 Flask_APP 环境变量 - Flask_APP environment variable not found 无法导入“D”:FLASK_APP - Could not import "D": FLASK_APP flask.cli.NoAppException:无法在模块“flaskr”中找到 Flask 应用程序或工厂。 使用“FLASK_APP=flaskr:name 指定一个 - flask.cli.NoAppException: Failed to find Flask application or factory in module "flaskr". Use "FLASK_APP=flaskr:name to specify one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM