From 5c8fc9e26580b143454adea9b77f07419f215092 Mon Sep 17 00:00:00 2001 From: panw Date: Mon, 30 Mar 2026 19:26:16 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E8=AF=8A=E6=96=AD=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E7=9A=84=E6=B5=8B=E8=AF=95=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加诊断脚本 test_config.py,用于测试和验证环境变量配置的正确加载。 脚本检查当前工作目录、Python 环境、.env 文件位置、已加载的环境变量, 并测试 python-dotenv 和 pydantic-settings 的集成情况,以协助调试配置问题。 --- test_config.py | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 test_config.py diff --git a/test_config.py b/test_config.py new file mode 100644 index 0000000..71e34b0 --- /dev/null +++ b/test_config.py @@ -0,0 +1,104 @@ +"""Diagnostic script to test environment variable loading.""" +import os +import sys +from pathlib import Path + +# Set UTF-8 encoding for Windows console +if sys.platform == "win32": + import codecs + sys.stdout = codecs.getwriter("utf-8")(sys.stdout.buffer, "strict") + sys.stderr = codecs.getwriter("utf-8")(sys.stderr.buffer, "strict") + +print("=" * 60) +print("Config Diagnostic Script") +print("=" * 60) + +# Check current directory +print(f"\n1. Current working directory: {os.getcwd()}") + +# Check Python path +print(f"\n2. Python executable: {sys.executable}") +print(f" Python version: {sys.version}") + +# Check if we're running from backend directory +in_backend = Path.cwd().name == "backend" +print(f"\n3. Running from backend directory: {in_backend}") + +# Check .env file location +env_paths = [ + Path(".env"), + Path("../.env"), + Path.cwd().parent / ".env", + Path("C:/electron/git/.env") +] + +print(f"\n4. Looking for .env file:") +for p in env_paths: + exists = p.exists() + if exists: + print(f" ✓ FOUND: {p}") + # Show first few lines + with open(p, 'r', encoding='utf-8') as f: + lines = f.readlines()[:3] + print(f" Content preview:") + for line in lines: + print(f" {line.rstrip()}") + else: + print(f" ✗ NOT FOUND: {p}") + +# Check environment variables +print(f"\n5. Environment variables starting with 'GM_':") +gm_vars = {k: v for k, v in os.environ.items() if k.startswith('GM_')} +if gm_vars: + for k, v in gm_vars.items(): + # Truncate long values for display + display_v = v[:30] + "..." if len(v) > 30 else v + print(f" {k} = {display_v}") +else: + print(" (none found)") + +# Try python-dotenv if available +print(f"\n6. Testing python-dotenv:") +try: + from dotenv import load_dotenv + print(" python-dotenv is installed") + + # Try loading from different paths + for env_path in env_paths: + if env_path.exists(): + result = load_dotenv(env_path, override=True) + print(f" load_dotenv({env_path.name}): loaded {result} variables") + # Check what got loaded + after_vars = {k: v for k, v in os.environ.items() if k.startswith('GM_')} + print(f" GM_ vars after load: {len(after_vars)}") + break +except ImportError: + print(" python-dotenv NOT installed") + +# Try importing pydantic-settings +print(f"\n7. Testing pydantic-settings:") +try: + from pydantic_settings import BaseSettings, SettingsConfigDict + print(" pydantic-settings is installed") + + class TestSettings(BaseSettings): + model_config = SettingsConfigDict( + env_prefix='GM_', + env_file='.env', + env_file_encoding='utf-8', + ) + encrypt_key: str = "default" + api_token: str = "default" + + try: + s = TestSettings() + print(f" ✓ Settings loaded successfully") + print(f" encrypt_key: {s.encrypt_key[:20]}...") + print(f" api_token: {s.api_token[:20]}...") + except Exception as e: + print(f" ✗ Settings failed: {type(e).__name__}: {e}") + +except ImportError as e: + print(f" pydantic-settings NOT installed: {e}") + +print("\n" + "=" * 60)