load_env.py 572 B

1234567891011121314151617
  1. """从 backend/.env 加载环境变量(供联调脚本使用)"""
  2. from pathlib import Path
  3. def load_backend_env() -> dict[str, str]:
  4. env_path = Path(__file__).resolve().parent.parent / '.env'
  5. result: dict[str, str] = {}
  6. if not env_path.exists():
  7. return result
  8. for line in env_path.read_text(encoding='utf-8').splitlines():
  9. line = line.strip()
  10. if not line or line.startswith('#') or '=' not in line:
  11. continue
  12. key, _, value = line.partition('=')
  13. result[key.strip()] = value.strip()
  14. return result