问题描述
我试图把我的配置设置分割成一个config.py文件中的不同类,如在烧瓶文件。
I'm attemping to split up my configuration settings into different classes within a config.py file, as documented here
I've split my project up into folders like so:
/bloggy
autoapp.py
config.py
/app
__init__.py
models.py
views.py
/static
/templates
My config.py
looks as follows
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or 'secret key'
SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
class TestingConfig(Config):
TESTING = True
config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'default': DevelopmentConfig
}
I have an application factory file autoapp.py
to create my Flask application from the desired environment variable
import os
from app import create_app
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
My app/__init__.py
looks as follows:
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])
db.init_app(app)
return app
After running
$ export FLASK_APP=autoapp.py
and
$ export FLASK_DEBUG=1
and starting the server with:
$ flask run
I get the following error:
Traceback (most recent call last):
File "/home/jwe/.local/lib/python3.5/site-packages/flask/cli.py", line 178, in __call__
self._flush_bg_loading_exception()
File "/home/jwe/.local/lib/python3.5/site-packages/flask/cli.py", line 166, in _flush_bg_loading_exception
reraise(*exc_info)
File "/home/jwe/.local/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/jwe/.local/lib/python3.5/site-packages/flask/cli.py", line 155, in _load_app
self._load_unlocked()
File "/home/jwe/.local/lib/python3.5/site-packages/flask/cli.py", line 170, in _load_unlocked
self._app = rv = self.loader()
File "/home/jwe/.local/lib/python3.5/site-packages/flask/cli.py", line 231, in load_app
rv = locate_app(self.app_import_path)
File "/home/jwe/.local/lib/python3.5/site-packages/flask/cli.py", line 95, in locate_app
'is .py' % module)
flask.cli.NoAppException: The file/path provided (autoapp) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py
Is there anyway I can get my application to use the settings laid out in config.py
or should I take a different approach?
Maybe adding the name of the flask application will help. Like following:
app.config.from_object(bloggy.config[config_name])
See here in flask doc.
这篇关于烧瓶flask.cli.NoAppException与应用程序工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!