python - 如何在配置文件中设置 FLASK_ENV?

标签 python flask

TL;DR

我不想 export我每次登录时的变量。我希望它们在我的配置中。

从文档

Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged.



无论如何我该怎么做?这怎么可能'?

更多 - 我尝试过的

我有一个 flask.cfgFLASK_ENV='development' .我跑flask run我得到 development打印。但是生产服务器已经启动了。 terminal它忽略以下内容:
app.config.from_pyfile("flask.cfg")

print(f"{app.config['ENV']}") => development

这是一个无关紧要的个人项目。这就是为什么我为了方便而忽略了最佳实践。

最佳答案

2020 年 3 月更新:

根据Flask devs ,你不能再这样做了:

The ENV and DEBUG config values are special because they may behave inconsistently if changed after the app has begun setting up. In order to set the environment and debug mode reliably, Flask uses environment variables.

The environment is used to indicate to Flask, extensions, and other programs, like Sentry, what context Flask is running in. It is controlled with the FLASK_ENV environment variable and defaults to production.

Setting FLASK_ENV to development will enable debug mode. flask run will use the interactive debugger and reloader by default in debug mode. To control this separately from the environment, use the FLASK_DEBUG flag.

To switch Flask to the development environment and enable debug mode, set FLASK_ENV:


> $ export FLASK_ENV=development 
> $ flask run (On Windows, use set instead of export.)

Using the environment variables as described above is recommended. While it is possible to set ENV and DEBUG in your config or code, this is strongly discouraged. They can’t be read early by the flask command, and some systems or extensions may have already configured themselves based on a previous value.

关于python - 如何在配置文件中设置 FLASK_ENV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600434/

相关文章:

python - numpy.linalg.det 的时间复杂度是多少?

python - Robot Framework 使用 PUDB 调试 python 代码

python - 雅虎 OAuth API 拒绝我的访问 token 时间戳 (Python)

python - 导入错误 : No module named flask. ext.sqlalchemy 在 vi​​rtualenv

python - 如何从 celery 后端存储中检索元

python - 列出可用的相机 OpenCV/Python

python - 将numpy数组转换为带分隔符的字符串并保存到txt文件

python - 测试 Flask 路由是否存在

python - flask -wtforms : Validation always false

python - 如何理解flask的这段代码?