简体   繁体   English

为什么Flask无法从Apache(mod_wsgi)看我的环境变量?

[英]Why can't Flask can't see my environment variables from 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. 我想通过Apache + mod_wsgi传递环境变量,以告诉我的应用程序是在开发环境还是生产环境中运行。 (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>

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: 根据对“ Apache SetEnv无法与mod_wsgi正常工作 ”的答案,我安装了run.py和主要的__init__.py如下所示:

Old run.py: 旧的run.py:

from myapp import app as application

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

New run.py : 新的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 __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

Two problems 两个问题

  1. This does not actually work. 这实际上是行不通的。 Within __init__.py , there is no 'ENVTYPE' key in os.envrion . __init__.py ,存在没有“ENVTYPE”键os.envrion Why not? 为什么不?

  2. 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. 我也不知道如何解决if __name__ == '__main__'部分,以便可以在PC run.py作为本地Flask应用程序运行。 What I put in my new run.py works, but only by calling _application instead of the wrapper function application . 我在新的run.py有效,但是只能通过调用_application而不是wrapper函数application As a result _application doesn't have access to the environment variables I defined. 结果, _application无法访问我定义的环境变量。 How can I fix that line? 我该如何解决?

Thank you! 谢谢!

I solved problem #1 thanks to this tip 由于这个技巧,我解决了问题#1

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

So the working version of run.py is: 因此, 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)

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. 我仍然没有解决问题2,但是-我不知道如何直接调用程序( if __name__ == '__main__' )并可以访问ENVTYPE环境变量。 Suggestions would still be appreciated. 建议将不胜感激。 Maybe I'll break that issues out into its own StackOverflow question. 也许我将把这个问题分解成自己的StackOverflow问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM