0
Hello, I’d like your help, because I’m trying to understand why you don’t perform!?
config.py
import os
import random, string
class Config(object):
CSRF_ENABLED = True
SECRET = '87725E7FC56B98E656AB7A4AEF9D01CAAA625FCF2F7133A48AD42F500E939884'
TEMPLATE_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
APP = None
class DevelopmentConfig(Config):
TESTING = False
DEBUG = True
IP_HOST = 'localhost'
PORT_HOST = 8000
URL_MAIN = 'http://%s:%s/' % (IP_HOST, PORT_HOST)
class TestingConfig(Config):
TESTING = True
DEBUG = True
IP_HOST = 'localhost' # Aqui geralmente é um IP de um servidor na nuvem e não o endereço da máquina local
PORT_HOST = 5000
URL_MAIN = 'http://%s:%s/' % (IP_HOST, PORT_HOST)
class ProductionConfig(Config):
DEBUG = False
TESTING = False
IP_HOST = 'localhost' # Aqui geralmente é um IP de um servidor na nuvem e não o endereço da máquina local
PORT_HOST = 8080
URL_MAIN = 'http://%s:%s/' % (IP_HOST, PORT_HOST)
app_config = {
'development': DevelopmentConfig(),
'testing': TestingConfig(),
'production': ProductionConfig()
}
app_active = os.getenv('FLASK_ENV')
app py.
# -*- coding: utf-8 -*-
from flask import Flask
# config import
from config import app_config, app_active
config = app_config[app_active]
def create_app(config_name):
app = Flask(__name__, template_folder='templates')
app.secret_key = config.SECRET
app.config.from_object(app_config[config_name])
app.config.from_pyfile('config.py')
@app.route('/')
def index():
return 'Meu primeiro run'
return app
run py.
from importlib import reload
from app import create_app
from config import app_config, app_active
config = app_config[app_active]
config.APP = create_app(app_active)
if __name__ == '__main__':
config.APP.run(host=config.IP_HOST, port=config.PORT_HOST)
When I perform with python run.py
the following error is returned.
Traceback (most recent call last):
File "D:/Programação/Projetos/GitHub/python/flask_livro/run.py", line 4, in <module>
from app import create_app
File "D:\Programação\Projetos\GitHub\python\flask_livro\app.py", line 7, in <module>
config = app_config[app_active]
KeyError: None
Would you kindly help me resolve?
You own the
FLASK_ENV
set in your environment?– Paulo Marques
Yes, I used the
set FLASK_ENV=development
– Lucas Corte
I remade the process and it worked
– Lucas Corte