File size: 4,294 Bytes
3998131 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
"""
Diagnostic script to help troubleshoot Pinecone API key issues
This will show you exactly what's happening with your environment variables
"""
import os
import sys
from pathlib import Path
print("=" * 80)
print("Pinecone API Key Diagnostic Tool")
print("=" * 80)
print()
# Check 1: Direct environment variable
print("1. Checking environment variable directly:")
print("-" * 80)
env_value = os.getenv("PINECONE_API_KEY")
if env_value:
masked = env_value[:8] + "..." + env_value[-4:] if len(env_value) > 12 else "***"
print(f" β Found: {masked}")
print(f" Length: {len(env_value)} characters")
else:
print(" β NOT FOUND in environment")
print()
# Check 2: .env file
print("2. Checking for .env file:")
print("-" * 80)
base_dir = Path(__file__).parent.parent
env_file = base_dir / ".env"
if env_file.exists():
print(f" β Found .env file at: {env_file}")
# Try to read it
try:
with open(env_file, 'r') as f:
content = f.read()
if "PINECONE_API_KEY" in content:
print(" β PINECONE_API_KEY found in .env file")
# Extract the value (simple parsing)
for line in content.split('\n'):
if line.strip().startswith("PINECONE_API_KEY"):
key_part = line.split('=', 1)[1].strip().strip('"').strip("'")
if key_part:
masked = key_part[:8] + "..." + key_part[-4:] if len(key_part) > 12 else "***"
print(f" Value (masked): {masked}")
else:
print(" β PINECONE_API_KEY NOT found in .env file")
except Exception as e:
print(f" β Could not read .env file: {e}")
else:
print(f" β .env file NOT found at: {env_file}")
print(f" Expected location: {env_file}")
print()
# Check 3: Try loading with dotenv
print("3. Testing dotenv loading:")
print("-" * 80)
try:
from dotenv import load_dotenv
print(" β python-dotenv is installed")
# Clear the variable first
if "PINECONE_API_KEY" in os.environ:
del os.environ["PINECONE_API_KEY"]
# Try loading
if env_file.exists():
load_dotenv(env_file, override=True)
after_load = os.getenv("PINECONE_API_KEY")
if after_load:
masked = after_load[:8] + "..." + after_load[-4:] if len(after_load) > 12 else "***"
print(f" β After loading .env: {masked}")
else:
print(" β Still not found after loading .env")
else:
print(" β No .env file to load")
except ImportError:
print(" β python-dotenv is NOT installed")
print(" Install with: pip install python-dotenv")
print()
# Check 4: What config.py sees
print("4. What config.py sees:")
print("-" * 80)
try:
# Import after potential dotenv load
from module_a.config import PINECONE_API_KEY
if PINECONE_API_KEY:
masked = PINECONE_API_KEY[:8] + "..." + PINECONE_API_KEY[-4:] if len(PINECONE_API_KEY) > 12 else "***"
print(f" β config.PINECONE_API_KEY: {masked}")
else:
print(" β config.PINECONE_API_KEY is empty/not set")
except Exception as e:
print(f" β Error importing config: {e}")
print()
# Check 5: Recommendations
print("5. Recommendations:")
print("-" * 80)
if not env_value and not env_file.exists():
print(" β Set the environment variable in your current terminal:")
print(" PowerShell: $env:PINECONE_API_KEY='your-key'")
print(" CMD: set PINECONE_API_KEY=your-key")
print()
print(" β OR create a .env file in project root with:")
print(" PINECONE_API_KEY=your-key")
elif env_file.exists() and "PINECONE_API_KEY" not in open(env_file).read():
print(" β Add PINECONE_API_KEY to your .env file:")
print(" PINECONE_API_KEY=your-key")
elif env_value:
print(" β Environment variable is set!")
print(" β Make sure you restart your application after setting it")
print(" β Run: python -m module_a.check_vector_db")
else:
print(" β Check that the API key value is not empty")
print(" β Make sure there are no extra spaces or quotes")
print()
print("=" * 80)
print("Diagnostic Complete")
print("=" * 80)
|