test: 添加用于诊断环境变量加载的测试脚本
添加诊断脚本 test_config.py,用于测试和验证环境变量配置的正确加载。 脚本检查当前工作目录、Python 环境、.env 文件位置、已加载的环境变量, 并测试 python-dotenv 和 pydantic-settings 的集成情况,以协助调试配置问题。
This commit is contained in:
104
test_config.py
Normal file
104
test_config.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user