agent / python /helpers /dotenv.py
Leon4gr45's picture
Add dotenv.py helper to fix ImportError
6b16661 verified
"""
Simple dotenv implementation for loading environment variables from .env files.
This is a compatibility layer for the agent-zero framework.
"""
import os
import re
from typing import Optional, Dict
def load_dotenv(path: Optional[str] = None) -> bool:
"""
Load environment variables from a .env file.
Args:
path: Path to the .env file. If None, looks for .env in current directory.
Returns:
True if the file was loaded successfully, False otherwise.
"""
if path is None:
path = ".env"
if not os.path.exists(path):
return False
try:
with open(path, "r") as f:
for line in f:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith("#"):
continue
# Match KEY=VALUE or KEY="VALUE" or KEY='VALUE'
match = re.match(r'^([A-Za-z_][A-Za-z0-9_]*)=(.*)$', line)
if match:
key = match.group(1)
value = match.group(2)
# Remove quotes if present
if (value.startswith('"') and value.endswith('"')) or \
(value.startswith("'") and value.endswith("'")):
value = value[1:-1]
# Set environment variable
os.environ[key] = value
return True
except Exception:
return False