From 9dc69a931a4c82b01b83bbf14b999ecb8037c3cc Mon Sep 17 00:00:00 2001 From: Surapong Kanoktipsatharporn Date: Mon, 20 Oct 2025 15:39:17 +0700 Subject: [PATCH] feat: Update check_env_setup to load and verify .env file - Now checks for .env file existence and location - Loads environment variables from .env file automatically - Provides fallback .env parser if python-dotenv not installed - Shows which API keys are configured from .env - Gives specific guidance based on what's found/missing --- check_env_setup.py | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) mode change 100644 => 100755 check_env_setup.py diff --git a/check_env_setup.py b/check_env_setup.py old mode 100644 new mode 100755 index d870649b..29dc7d74 --- a/check_env_setup.py +++ b/check_env_setup.py @@ -6,6 +6,33 @@ Checks if the necessary API keys are configured for your provider combination. import os import sys +from pathlib import Path + +# Try to import dotenv, but provide fallback if not available +try: + from dotenv import load_dotenv + + HAS_DOTENV = True +except ImportError: + HAS_DOTENV = False + + def load_dotenv(path): + """Simple fallback .env file parser if python-dotenv is not installed.""" + if not path.exists(): + return + + with open(path, "r") as f: + for line in f: + line = line.strip() + # Skip comments and empty lines + if not line or line.startswith("#"): + continue + # Parse KEY=VALUE + if "=" in line: + key, value = line.split("=", 1) + key = key.strip() + value = value.strip().strip('"').strip("'") + os.environ[key] = value def check_api_keys(): @@ -14,7 +41,34 @@ def check_api_keys(): print("TradingAgents - Environment Setup Checker") print("=" * 70) - # Check for common API keys + # Check for .env file + env_file = Path(".env") + env_example_file = Path(".env.example") + + print("\n0. Environment File Status:") + print("-" * 70) + + if env_file.exists(): + print(f" ✅ .env file found: {env_file.absolute()}") + # Load .env file + load_dotenv(env_file) + if HAS_DOTENV: + print( + " Loaded environment variables from .env file (using python-dotenv)" + ) + else: + print( + " Loaded environment variables from .env file (using built-in parser)" + ) + else: + print(f" ⚠️ .env file not found: {env_file.absolute()}") + if env_example_file.exists(): + print(f" Hint: Copy .env.example to .env and add your API keys") + print(f" Command: cp .env.example .env") + else: + print(f" Create a .env file with your API keys") + + # Check for common API keys (after loading .env) api_keys = { "OPENAI_API_KEY": os.getenv("OPENAI_API_KEY"), "OPENROUTER_API_KEY": os.getenv("OPENROUTER_API_KEY"), @@ -81,11 +135,18 @@ def check_api_keys(): print(""" For OpenRouter (chat) + OpenAI (embeddings): - In your terminal or .env file: + Option A: Set in terminal (temporary): export OPENROUTER_API_KEY="sk-or-v1-..." export OPENAI_API_KEY="sk-proj-..." + Option B: Add to .env file (permanent - recommended): + + Create/edit .env file in this directory with: + + OPENROUTER_API_KEY=sk-or-v1-... + OPENAI_API_KEY=sk-proj-... + In your config: config = {