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
相关推荐
geovindu21 小时前
python: Memento Pattern
开发语言·python·设计模式·备忘录模式
寻星探路21 小时前
【JVM 终极通关指南】万字长文从底层到实战全维度深度拆解 Java 虚拟机
java·开发语言·jvm·人工智能·python·算法·ai
lbb 小魔仙21 小时前
【Java】Java 实战项目:手把手教你写一个电商订单系统
android·java·python
岱宗夫up21 小时前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
Dxy123931021621 小时前
中文乱码恢复方案
开发语言·python
rongyili881 天前
Dify 外部知识库集成 Milvus 实战指南
开发语言·python·milvus
Hello eveybody1 天前
什么是动态规划(DP)?(Python版)
python·动态规划
南 阳1 天前
Python从入门到精通day34
开发语言·python
APIshop1 天前
阿里巴巴中国站按图搜索1688商品(拍立淘)API 返回值说明
java·python·图搜索算法
哈库纳玛塔塔1 天前
dbVisitor 利用 queryForPairs 让键值查询一步到位
java·数据库·python