.env相关配置案例
#!/usr/bin/env python# -*- coding: utf-8 -*-""" @Time : 2025/5/14 10:44 @Author : thezehui@gmail.com @File : config.py """fromfunctoolsimportlru_cachefromtypingimportOptionalfrompydantic_settingsimportBaseSettings,SettingsConfigDictclassSettings(BaseSettings):"""MoocManus后端中控配置信息,从.env或者环境变量中加载数据"""# 项目基础配置env:str="development"log_level:str="INFO"# 日志等级app_config_filepath:str="config.yaml"# 数据库相关配置sqlalchemy_database_uri:str="postgresql+asyncpg://postgres:postgres@localhost:5432/manus"# Redis缓存配置redis_host:str="localhost"redis_port:int=6379redis_db:int=0redis_password:str|None=None# Cos腾讯云对象存储配置cos_secret_id:str=""cos_secret_key:str=""cos_region:str=""cos_scheme:str="https"cos_bucket:str=""cos_domain:str=""# Sandbox配置sandbox_address:Optional[str]=Nonesandbox_image:Optional[str]=Nonesandbox_name_prefix:Optional[str]=Nonesandbox_ttl_minutes:Optional[int]=60sandbox_network:Optional[str]=Nonesandbox_chrome_args:Optional[str]=""sandbox_https_proxy:Optional[str]=Nonesandbox_http_proxy:Optional[str]=Nonesandbox_no_proxy:Optional[str]=None# 使用pydantic v2的写法来完成环境变量信息的告知model_config=SettingsConfigDict(env_file=".env",env_file_encoding="utf-8",extra="ignore",)@lru_cache()defget_settings()->Settings:"""获取当前MoocManus项目的配置信息,并对内容进行缓存,避免重复读取"""settings=Settings()returnsettingsif__name__=="__main__":sttings=Settings()print(sttings)
![]()