如何在配置文件中设置FLASK_ENV? [英] How to set FLASK_ENV inside config file?

查看:87
本文介绍了如何在配置文件中设置FLASK_ENV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR

我不想每次登录时都导出变量.我希望它们在我的配置中.

I don't want to export my variables every time I log on. I want them in my config.

来自文档

建议使用如上所述的环境变量.虽然可以在配置或代码中设置ENV和DEBUG,但强烈建议不要这样做.

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.

我该怎么办?怎么可能?

How do I do it anyway? How is it 'possible'?

更多-我尝试过的事情

我有一个 flask.cfg FLASK_ENV ='development'.我运行了 flask run ,并打印了 development .但是生产服务器已经启动.它将忽略以下内容:

I have a flask.cfg with FLASK_ENV='development'. I run flask run and I get development printed. But the production server has already started. It ignores the following:

app.config.from_pyfile("flask.cfg")

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

这是一个无关紧要的个人项目.因此,为了方便起见,我忽略了最佳做法.

This is in a personal project of no consequence. That's why I am ignoring best practices for convenience.

推荐答案

有关此问题:安装 python-dotenv 程序包,在项目根文件夹中创建一个.flaskenv文件,然后添加例如这个:

What about this: install python-dotenv package, create a .flaskenv file in your project root folder and add, for example, this:

FLASK_APP=app.py (or whatever you named it)
FLASK_ENV=development (or production)

保存.进行烧瓶运行.

这篇关于如何在配置文件中设置FLASK_ENV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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