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
相关推荐
啵啵鱼爱吃小猫咪25 分钟前
机械臂阻抗控制github项目-mujoco仿真
开发语言·人工智能·python·机器人
MaximusCoder27 分钟前
等保测评命令——Centos Linux
linux·运维·经验分享·python·安全·centos
yunyun3212328 分钟前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
m0_6625779730 分钟前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
songyuc44 分钟前
【PyTorch】感觉`CrossEntropyLoss`和`BCELoss`很类似,为什么它们接收labels的shape常常不一样呢?
人工智能·pytorch·python
ℳ๓₯㎕.空城旧梦1 小时前
Python单元测试(unittest)实战指南
jvm·数据库·python
浩子智控2 小时前
python程序打包的文件地址处理
开发语言·python·pyqt
Jackey_Song_Odd2 小时前
Part 1:Python语言核心 - 序列与容器
开发语言·windows·python
m0_662577972 小时前
Python迭代器(Iterator)揭秘:for循环背后的故事
jvm·数据库·python