Flask为什么看不到来自Apache(mod_wsgi)的环境变量? [英] Why can't Flask can't see my environment variables from Apache (mod_wsgi)?

查看:91
本文介绍了Flask为什么看不到来自Apache(mod_wsgi)的环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Apache + mod_wsgi传递环境变量,以告诉我的应用程序是在开发环境还是生产环境中运行. (这需要在启动应用程序时发生,然后再发出任何请求.)例如:

I want to pass in environment variables through Apache + mod_wsgi to tell my app whether it's running in a development or production environment. (This needs to happen when the app is launched, before any requests have come in.) For example:

<VirtualHost *:80>
    ...
    SetEnv ENVTYPE production
    WSGIScriptAlias /myapp  /apps/www/80/wsgi-scripts/myapp/run.py
</VirtualHost>
<VirtualHost *:8080>
    ...
    SetEnv ENVTYPE development
    WSGIScriptAlias /myapp  /apps/www/80/wsgi-scripts/myapp/run.py
</VirtualHost>

基于对"> Apache SetEnv无效的回答如对mod_wsgi 所期望的那样,我已经设置了run.py和主__init__.py,如下所示:

Based on the answer given to "Apache SetEnv not working as expected with mod_wsgi", I have setup run.py and the main __init__.py like this:

from myapp import app as application

if __name__ == '__main__':
    application.run(debug=True, threaded=True)

新run.py:

import os
from myapp import app as _application

def application(environ, start_response):
    os.environ['ENVTYPE'] = environ['ENVTYPE']
    return _application(environ, start_response)

if __name__ == '__main__':
    _application.run(debug=True, threaded=True)

__ init __.py

app = Flask(__name__)
app.config.from_object(__name__)
if os.environ.get('ENVTYPE') == 'production'
    # Setup DB and other stuff for prod environment
else:
    # Setup DB and other stuff for dev environment

两个问题

  1. 这实际上不起作用.在__init__.py中,在os.envrion中没有"ENVTYPE"键.为什么不呢?

  1. This does not actually work. Within __init__.py, there is no 'ENVTYPE' key in os.envrion. Why not?

我也不知道如何修复if __name__ == '__main__'部分,以便可以在PC上将run.py作为本地Flask应用程序运行.我在新的run.py中添加的内容有效,但只能通过调用_application而不是包装函数application来实现.结果,_application无法访问我定义的环境变量.我该如何解决?

I also don't know how to fix the if __name__ == '__main__' section so that I can run run.py as a local Flask app on my PC. What I put in my new run.py works, but only by calling _application instead of the wrapper function application. As a result _application doesn't have access to the environment variables I defined. How can I fix that line?

谢谢!

推荐答案

由于

请注意,Flask应用程序已导入def应用程序块内-如果将其导入到def应用程序块外,则将无法在Flask应用程序级别使用环境变量或在应用程序加载时导入的任何文件.这是因为导入Flask应用程序时尚未加载WSGI应用程序,因此它还不能传递环境变量.

Note that the Flask app is imported inside the def application block — if you import it outside of this block, you won't be able to use the environment variables at the Flask app level or any file which is imported on application load. This is because the WSGI application hasn't loaded at the time you import the Flask application, so it can't pass the environment variables yet.

所以run.py的工作版本是:

import os

def application(environ, start_response):
    os.environ['ENVTYPE'] = environ['ENVTYPE']
    from myapp import app as _application
    return _application(environ, start_response)

if __name__ == '__main__':
    _application.run(debug=True, threaded=True)

我仍然没有解决问题2,但是-我不知道如何直接调用程序(if __name__ == '__main__')并可以访问ENVTYPE环境变量.建议仍将不胜感激.也许我将把这个问题分解成自己的StackOverflow问题.

I still haven't solved problem #2, however - I don't know how to call the program directly (if __name__ == '__main__') and have access to the ENVTYPE environment variable. Suggestions would still be appreciated. Maybe I'll break that issues out into its own StackOverflow question.

这篇关于Flask为什么看不到来自Apache(mod_wsgi)的环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆