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.cfg
与 FLASK_ENV='development'
.我跑flask run
我得到 development
打印。但是生产服务器已经启动了。 它忽略以下内容: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/