KeyError:APP_SETTINGS

时间:2016-10-04 18:22:07

标签: python python-3.x flask apache2 flask-sqlalchemy

我收到KeyError,这是apache2/error.log

tail /var/log/apache2/error.log
[Tue Oct 04 18:07:13.305780 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852] mod_wsgi (pid=15459): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://localhost/login/
[Tue Oct 04 18:07:13.305918 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852] mod_wsgi (pid=15459): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://localhost/login/
[Tue Oct 04 18:07:13.306787 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852] Traceback (most recent call last):, referer: http://45.55.236.208/login/
[Tue Oct 04 18:07:13.306833 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852]   File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://localhost/login/
[Tue Oct 04 18:07:13.306842 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852]     from FlaskApp import app as application, referer: http://localhost/login/
[Tue Oct 04 18:07:13.306855 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 14, in <module>, referer: http://localhost/login/
[Tue Oct 04 18:07:13.306861 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852]     app.config.from_object(os.environ['APP_SETTINGS']), referer: http://localhost/login/
[Tue Oct 04 18:07:13.306872 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852]   File "/usr/lib/python3.5/os.py", line 725, in __getitem__, referer: http://localhost/login/
[Tue Oct 04 18:07:13.306892 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852]     raise KeyError(key) from None, referer: http://localhost/login/
[Tue Oct 04 18:07:13.306925 2016] [wsgi:error] [pid 15459:tid 140052675933952] [client 174.58.31.189:56852] KeyError: 'APP_SETTINGS', referer: http://localhost/login/

在我的主要应用程序中,我有:

import os
app.config.from_object(os.environ['APP_SETTINGS'])

并且,这是我的config.py

# default config
import os

class BaseConfig(object):
    DEBUG = False
    SECRET_KEY = 'jjjjjjj'
    SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']


class DevelopmentConfig(BaseConfig):
    DEBUG = True


class ProductionConfig(BaseConfig):
    DEBUG = False

我做了什么:

export APP_SETTINGS="config.DevelopmentConfig"
export DATABASE_URL="postgresql://user:mypassword@localhost/database"

它没有告诉我哪个键丢失或出了什么问题,我正在从digitalocean ubuntu运行这个应用程序,如果有帮助的话。

1 个答案:

答案 0 :(得分:0)

没有办法在Apache的启动环境中轻松设置环境变量。您通常会显式地从WSGI脚本文件中设置它们,或者通过将它们放在某个单独的文件中间接设置它们并读取WSGI脚本文件并根据该单独文件中设置的内容为它们设置它们。使用单独的文件是您不希望代码存储库中的信息。该单独文件可能位于/etc/或其他位置,仅存在于您要部署的系统上。