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
相关推荐
小oo呆11 小时前
【学习心得】Python好库推荐——pipx
linux·开发语言·python
smile_Iris11 小时前
Day 28 元组和OS模块
python·机器学习
AI科技星12 小时前
时空运动的几何约束:张祥前统一场论中圆柱螺旋运动光速不变性的严格数学证明与物理诠释
服务器·数据结构·人工智能·python·科技·算法·生活
AIsdhuang12 小时前
2025 AI培训权威榜:深度评测与趋势前瞻
人工智能·python·物联网
m0_7269659812 小时前
RAG小实战
开发语言·python
只与明月听12 小时前
FastAPI入门实战
前端·后端·python
无心水12 小时前
【Python实战进阶】12、Python面向对象编程实战:从零构建搜索引擎,掌握封装、继承与多态!
开发语言·python·搜索引擎·python进阶·python面向对象·搜索引擎实战·封装继承多态
mortimer12 小时前
Python + FFmpeg 视频自动化处理指南:从硬件加速到精确剪辑
python·ffmpeg·音视频开发
帅得不敢出门12 小时前
Android8 Framework实现Ntp服务器多域名轮询同步时间
android·java·服务器·python·framework·github
haiyu_y12 小时前
Day 29 异常处理
python