python基础之dynaconf配置参数,背景、示例、方法(根据环境动态配置参数)

python基础之ConfigParser配置参数,背景、示例、方法

dynaconf
  • 目录结构

    cpp 复制代码
    settings
    ├── __init__.py
    ├── constant.py
    ├── setting.py
    ├── settings.dev.toml
    └── settings.production.toml
  • setting.py

    clike 复制代码
    import os
    from dynaconf import Dynaconf
    
    settings_dir = os.path.dirname(os.path.abspath(__file__))
    config = Dynaconf(
        env=os.environ.get("ENVIRONMENT") or "development",
        settings_files=[
        	os.path.join(settings_dir, "settings.setting.toml"), # 开发、生产环境公用配置
            os.path.join(settings_dir, "settings.dev.toml"),        # 开发环境
            os.path.join(settings_dir, "settings.production.toml"), # 生产环境
        ],
        environments=True, # 是否使用多环境
    )
  • settings.setting.toml

    复制代码
    [default]
    port = 8080
  • settings.dev.toml

    csharp 复制代码
    [development]
    DB_HOST='127.0.0.1'
    DB_PORT=6379
    DB_USER='document_dev'
    DB_PASSWD= '123456'
  • settings.production.toml

    csharp 复制代码
    [production]
    DB_HOST='127.0.0.1'
    DB_PORT=6379
    DB_USER='document_prod'
    DB_PASSWD= '123456'
  • 如何引用

    cpp 复制代码
     db_host = config.DB_HOST 
    复制代码
    toml 文件中小写,也要改成引用时也该改成大写
    cpp 复制代码
    ENVIRONMENT=production uvicorn main:app --reload
    ENVIRONMENT=dev uvicorn main:app --host 0.0.0.0 --port 8083 --reload
相关推荐
斯维赤4 分钟前
Python学习超简单第十一弹:邮件发送
开发语言·python·学习
qq_372154235 分钟前
如何配置表中某列的排序权重_全文索引配置与权重分配
jvm·数据库·python
还是阿落呀12 分钟前
如何判断一个年份是否为闰年?
python
2501_9142459316 分钟前
CSS如何使用-nth-of-type精确选择列表项_通过元素类型限制提升样式健壮性
jvm·数据库·python
overmind18 分钟前
oeasy Python 124 序列_字符串_string_str
开发语言·python
吕源林23 分钟前
Golang如何做本地缓存加速_Golang本地缓存教程【核心】
jvm·数据库·python
_深海凉_23 分钟前
LeetCode热题100-26. 删除有序数组中的重复项
python·算法·leetcode
武帝为此24 分钟前
【热卡填充法介绍】
人工智能·python·机器学习
片酷27 分钟前
【Isaacsim&Isaaclab】安装教程
linux·开发语言·python
RunsenLIu36 分钟前
019 | backtrader回测布林带突破策略
开发语言·python