Skip to main content
 首页 » 编程设计

python之如何覆盖 Flask 配置中的配置参数

2025年05月04日21cmt

我正在使用 flask config 并像这样配置我的应用程序:

app = Flask(__name__) 
app.config.from_object('yourapplication.Config') 

我的配置对象是这样的:

class Config(object): 
    DEBUG = True 
    TESTING = False 
    DB_USER = os.getenv("DB_USER", "db_user") 
    DB_PASSWORD = os.getenv("DB_PASSWORD", "db_password") 
    DB_HOST = os.getenv("DB_HOST", "localhost")  # 127.0.0.1" 
    DB_PORT = os.getenv("DB_PORT", "5555") 
    DB_SCHEMA = "my_schema" 
    DB_DATABASE_NAME = "my_database" 
    SQLALCHEMY_DATABASE_URI = "postgresql://{}:{}@{}:{}/{}".format( 
        DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_DATABASE_NAME) 
    SQLALCHEMY_TRACK_MODIFICATIONS = False 

我这样运行我的应用程序(使用 flask 脚本):

my_flask_app cmd1 arg1 arg2 

我想做以下事情:

DEBUG=true DB_PORT=1234 my_flask_app cmd1 arg1 arg2 

并覆盖 2 个配置参数。我想知道是否已经有一种方法可以从命令行覆盖配置参数,或者我是否应该去写一些代码?

编辑:为了澄清,我有几个继承自“Config(object)”的预设配置,如下所示:

class Dev(Config): 
    pass 
 
class Ua(Config): 
    pass 
 
class Prod(Config): 
    DEBUG = False 

我的应用程序默认为“开发”配置。很容易想象您想要运行“UA”配置但覆盖端口的场景,如下所示:

FLASK_ENV=UA DB_PORT=1234 my_flask_app cmd1 arg1 arg2 

“FLASK_ENV”将在内部选择“UA”配置对象,但我希望在顶部应用 DB_PORT。只是想知道是否有扩展或有人对此有任何想法。

请您参考如下方法:

有几种方法可以覆盖。

1) 使用 config.from_envvar + config.cfg。示例:

# test.py 
import json 
 
from flask import Flask, current_app 
 
 
class Config(object): 
    DEBUG = True 
    TESTING = False 
    DB_PORT = 1234 
 
 
app = Flask(__name__) 
app.config.from_object('test.Config') 
app.config.from_envvar('YOURAPPLICATION_SETTINGS', silent=True) 
 
 
@app.route('/') 
def index(): 
    return json.dumps({ 
        'DEBUG': current_app.config['DEBUG'], 
        'DB_PORT': current_app.config['DB_PORT'], 
    }) 
 
 
if __name__ == '__main__': 
    app.run() 

config.cfg:

DB_PORT = 4321 
DEBUG = False 

运行我们的应用程序:python test.py 并打开 http://127.0.0.1:5000/。您将看到配置的默认值:

{"DEBUG": true, "DB_PORT": 1234} 

现在将 config.cfg 的路径添加到 YOURAPPLICATION_SETTINGS 变量中并重新启动应用程序。

export YOURAPPLICATION_SETTINGS={full_path_to}/config.cfg 
python test.py 
# open http://127.0.0.1:5000/  
# you will see {"DEBUG": false, "DB_PORT": 4321} 

2) 与第一种方式类似,但没有.cfg。 测试.py:

import json 
import os 
 
from flask import Flask, current_app 
 
 
class Config(object): 
    DEBUG = True 
    TESTING = False 
    DB_PORT = 1234 
 
 
class ProductionConfig(Config): 
    DEBUG = False 
    DB_PORT = 4321 
 
 
class TestingConfig(Config): 
    TESTING = True 
 
 
app = Flask(__name__) 
app.config.from_object(os.environ.get('CONFIG_CLASS', 'test.Config')) 
# route + run ... 

检查方法:

python test.py # open http://127.0.0.1:5000/ - default config 
# ProductionConfig config 
export CONFIG_CLASS=test.ProductionConfig 
python test.py # open http://127.0.0.1:5000/ - production config 

3) 在某些情况下,仅环境变量就足够了。

But be careful you can have problems with types

import json 
import os 
 
from flask import Flask, current_app 
 
 
class Config(object): 
    DEBUG = True 
    TESTING = False 
    DB_PORT = 1234 
 
 
app = Flask(__name__) 
app.config.from_object('test.Config') 
app.config.update({ 
    'DB_PORT': os.environ.get('DB_PORT', Config.DB_PORT) 
    # you should to set type of variable here 
    # DB_PORT will be string 
    # 'DB_PORT': int(os.environ.get('DB_PORT', Config.DB_PORT)) 
}) 
# route + run ... 

让我们检查一下:

export DB_PORT=4321 
python test.py 

打开http://127.0.0.1:5000/您会看到 DB_PORT 已更改但不是整数。因此,对于某些设置和特定情况,这是一个很好的解决方案。我认为 1 和 2 方式更好。

希望这对您有所帮助。