使用环境变量作为应用程序配置运行Flask软件包

时间:2019-11-30 04:28:44

标签: python flask

direnv文件:

source venv/bin/activate

export FLASK_APP=bookgress
export APP_SETTINGS="config.DevelopmentConfig"

config.py看起来像这样:

import os
basedir = os.path.abspath(os.path.dirname(__file__))


class Config(object):
    DEBUG=False
    TESTING=False
    CSRF_ENABLED=True
    SECRET_KEY=b'secret'


class ProductionConfig(Config):
    DEBUG = False


class StagingConfig(Config):
    DEVELOPMENT = True
    DEBUG = True


class DevelopmentConfig(Config):
    DEVELOPMENT = True
    DEBUG = True


class TestingConfig(Config):
    TESTING = True

APP_SETTINGS确实设置好了,但是来自我的__init__.py,看起来像这样:

from flask import Flask

app = Flask('bookgress')
app.config.from_envvar('APP_SETTINGS')

import bookgress.views

我得到一个错误:

$❯ flask run
 * Serving Flask app "bookgress"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)

    rv = self.invoke(ctx)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 305, in __init__
    self._load_unlocked()
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/__init__.py", line 4, in <module>
    app.config.from_envvar('APP_SETTINGS')
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/config.py", line 111, in from_envvar
    return self.from_pyfile(rv, silent=silent)
  File "/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/venv/lib/python3.6/site-packages/flask/config.py", line 131, in from_pyfile
    with open(filename, mode="rb") as config_file:
FileNotFoundError: [Errno 2] Unable to load configuration file (No such file or directory): '/home/mustaqim/Library/Cloud/code/cs50/pset9/project/bookgress/config.DevelopmentConfig'

总体布局如下:

.
├── config.py
├── helpers.py
├── __init__.py
├── README.md
├── requirements.txt
├── static
│   ├── css
│   │   ├── all.min.css
│   │   └── styles.css
│   ├── logo.png
│   ├── media
│   │   ├── bookgress.svg
│   │   ├── errors
│   │   │   └── 404.svg
│   │   └── favicon.ico
│   └── webfonts
│       └── fa-solid-900.woff2
├── templates
│   ├── errors
│   │   ├── 404.html
│   │   └── 500.html
│   ├── index.html
│   ├── layout.html
│   └── register.html
├── venv ( With many more files in)
└── views.py

1 个答案:

答案 0 :(得分:0)

根据documentation from_envvar从指向配置文件的环境变量中加载配置,这基本上只是一个快捷方式,其中包含针对from_pyfile的更好的错误消息。 from_envvarfrom_pyfile的示例配置如下所示。

my_app_config.cfg或my_app_config.py

DB_URI="Your_DB_URI_String"
.
.
Debug=False

但是您使用的是不同的配置模式。您应该使用from_object(文档here)。