From efad980f67bdb6637d8454f402aacd30262e5b28 Mon Sep 17 00:00:00 2001 From: mzbgf Date: Tue, 12 May 2026 20:32:14 +0800 Subject: [PATCH] fix: load .env from CWD with upward fallback in non-interactive CLI mode load_dotenv() without arguments relies on find_dotenv() to locate the .env file. The find_dotenv() function decides the search starting point based on the environment: - Interactive (REPL, python -c): _is_interactive() returns True -> starts from os.getcwd() (user's working directory) -> works - Non-interactive (installed CLI entry point via pip/uv): _is_interactive() returns False -> walks up the call stack from the caller's frame, which resolves to site-packages/litellm/ -> never finds the user's .env Fix: pass find_dotenv(usecwd=True) to load_dotenv(), anchoring the search to CWD while preserving python-dotenv's upward directory traversal for users who keep .env in a parent directory. --- litellm/__init__.py | 2 +- litellm/proxy/proxy_cli.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/litellm/__init__.py b/litellm/__init__.py index e1b367fb234..e292e9496cf 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -17,7 +17,7 @@ import os import dotenv as _dotenv if os.getenv("LITELLM_MODE", "DEV") == "DEV": - _dotenv.load_dotenv() + _dotenv.load_dotenv(dotenv_path=_dotenv.find_dotenv(usecwd=True)) from typing import ( Callable, diff --git a/litellm/proxy/proxy_cli.py b/litellm/proxy/proxy_cli.py index 5fc8c44b2d8..d4c60dda85c 100644 --- a/litellm/proxy/proxy_cli.py +++ b/litellm/proxy/proxy_cli.py @@ -10,7 +10,7 @@ from typing import TYPE_CHECKING, Any, Optional, Union import click import httpx -from dotenv import load_dotenv +from dotenv import find_dotenv, load_dotenv import litellm from litellm.constants import DEFAULT_NUM_WORKERS_LITELLM_PROXY @@ -27,7 +27,7 @@ config_filename = "litellm.secrets" litellm_mode = os.getenv("LITELLM_MODE", "DEV") # "PRODUCTION", "DEV" if litellm_mode == "DEV": - load_dotenv() + load_dotenv(find_dotenv(usecwd=True)) from enum import Enum telemetry = None