Python 如何从 Flask 中的配置文件导入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15122312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import from config file in Flask?
提问by Siecje
I have followed the layout of my Flask project from http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world.
我遵循了http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world中我的 Flask 项目的布局。
I have the following structure:
我有以下结构:
app/
__init__.py
views.py
forms.py
myFile.py
run.py
config.py
In views.py, forms.py I am able to use
在views.py、forms.py中我可以使用
from config import basedir
However I cannot use that in myFile.py
但是我不能在 myFile.py 中使用它
I added
我加了
import Flask
and when I modify it, the Flask web server restarts, but it doesn't say found changes in app/myFile.py restarting it just restarts.
当我修改它时,Flask Web 服务器会重新启动,但它并没有说在 app/myFile.py 中找到更改重新启动它只是重新启动。
What do I need to do to be able to use
我需要做什么才能使用
from config import basedir
in my python file. I don't see anything special being done in __init__.pyfor forms.py.
在我的python文件中。我没有看到__init__.py为 forms.py做任何特别的事情。
EDIT: This is my __init__.pyfile:
编辑:这是我的__init__.py文件:
from flask import Flask
from config import basedir
app = Flask(__name__)
app.config.from_object('config')
from app import views
采纳答案by Doobeh
When people talk about configs in Flask, they are generally talking about loading values into the app's configuration. In your above example you could have something like app.config.from_object('config')in your init.pyfile. Then all the configuration values will be loaded into the app.configdictionary.
当人们谈论 Flask 中的配置时,他们通常是在谈论将值加载到应用程序的配置中。在上面的示例app.config.from_object('config')中,您的init.py文件中可能有类似的内容。然后所有的配置值将被加载到app.config字典中。
Then in any of your files you could just import the app object to gain access to that dictionary. I tend to access that appobject by doing from flask import current_app as appthen just app.config['MY_SETTING']to get the value I care about. Read up more in the documenation.
然后在您的任何文件中,您只需导入 app 对象即可访问该字典。我倾向于app通过这样做来访问该对象,from flask import current_app as app只是app.config['MY_SETTING']为了获得我关心的价值。在文档中阅读更多内容。
回答by mrArias
After a little bit of fiddling (and a little help from the 'net), I could improve this further, by changing the code to include the config to:
经过一点点摆弄(以及来自'网络的一点帮助),我可以通过将代码更改为包含配置来进一步改进这一点:
app.config.from_object('config.ProductionConfig')
This enables this cool pattern for configurations:
这为配置启用了这种很酷的模式:
class Config(object):
DEBUG = True
DEVELOPMENT = True
SECRET_KEY = 'do-i-really-need-this'
FLASK_HTPASSWD_PATH = '/secret/.htpasswd'
FLASK_SECRET = SECRET_KEY
DB_HOST = 'database' # a docker link
class ProductionConfig(Config):
DEVELOPMENT = False
DEBUG = False
DB_HOST = 'my.production.database' # not a docker link
What's left now is to see how to integrate testing configs into this, but at least it feels less clumsy.
现在剩下的就是看看如何将测试配置集成到其中,但至少感觉不那么笨拙。
回答by Siva
I created a config.json file in my flask project - root folder, like below:
我在我的烧瓶项目 - 根文件夹中创建了一个 config.json 文件,如下所示:
config.json
配置文件
{
"mail_settings":{
"MAIL_SERVER": "smtp.gmail.com",
"MAIL_PORT": 465,
"MAIL_USE_TLS": "False",
"MAIL_USE_SSL": "True",
"MAIL_USERNAME": "[email protected]",
"MAIL_PASSWORD": "your_password"
},
"database":{
"MYSQL_HOST":"localhost",
"MYSQL_USER": "user_name",
"MYSQL_PASSWORD":"password",
"MYSQL_DB":"database_name"
}
}
and just add below code in where we want to access configuration values. for example, i am going to use in app.py like below:
只需在我们想要访问配置值的地方添加以下代码。例如,我将在 app.py 中使用,如下所示:
first import flask json library:
首先导入flask json库:
import json
second open the file to read and store the json in some variable like below:
其次打开文件以读取 json 并将其存储在一些变量中,如下所示:
with open('config.json') as config_file:
config_data = json.load(config_file)
add below code after creates a Flask application, named app app = Flask(__name__)like below, for access the config.json - configuration values.
在创建一个名为 app 的 Flask 应用程序后添加以下代码,app = Flask(__name__)如下所示,用于访问 config.json - 配置值。
# mail configuration
mail_settings = config_data['mail_settings']
app.config.update(mail_settings)
# database configuration
db_settings = config_data['database']
app.config.update(db_settings)

