Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app_config[config_name] getting KeyError null value for flask environment

I am creating employee management software in python in flask environement by referring this code

https://github.com/littlewonder/squadmaster

i have installed pip , flask an other relevant libraries .i have also created virtal environment .I put the project folder inside the flask as well .

folder structure in side flask folder is like this

enter image description here

when i tried to tun run.py . it gives me error

app.config.from_object(app_config[config_name]) KeyError: None

this is my run.py file

import os

from app import create_app

config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)

if __name__ == '__main__':
    app.run()

i have faced similar question

Flask does not load configuration

i have implemented its solution and use these strings

app.config.from_object('myapplication.default_settings')

app.config.from_object('my_app.config.{}'.format(config_name))

I have set FLASK_CONFIG as in upper as documented I have setup the FLASK_CONFIG as environment variable in system build path . but it results me to error as KeyError = "full path" .

i have also tried to set FLASK_CONFIG ='development' and FLASK_CONFIG =DevelopmentConfig

as mentioned in config.py

app_config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig
}

what else can i tried to get a hint whats going wrong . need some suggestion

like image 326
neeraj Avatar asked Sep 20 '25 05:09

neeraj


1 Answers

The first problem can be solved with a reasonable default:

config_name = os.getenv('FLASK_CONFIG') or 'default'

or just os.getenv('FLASK_CONFIG', 'default'), where the meaning of the default can be set in config.app_config

app_config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'default': ProductionConfig
}

The app_config maps a name to an object with configuration. A reference ina string form is also allowed, but you can pass the object directly to .from_object):

app.config.from_object(app_config[config_name])

The configuration data stored e.g. DevelopmentConfig must contain the data items as its attributes. It can be a class.

like image 114
VPfB Avatar answered Sep 21 '25 21:09

VPfB