2
votes

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. 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. 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. 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. 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. I tried export FLASK_CONFIG=development as my last attempt, to see if that would work correctly, and it did not. 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'

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. It's almost as if the app gets created with 'app = Flask(name)', skips the config loading, and goes to the Blueprint registration. 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. It just doesn't read from the environment. DEBUG=True doesn't work, but other configurations do. Perhaps the app instance is created before the environment is loaded with variables from .env. I will leave this here, and update with a solution when I find it.

app/__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

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

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

1 Answers

6
votes

A kind soul over at IRC #pocoo helped me see the light. The answer is so simple. The .env needs to be loaded before importing create_app, which also imports the configuration file.

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