fix: load_dotenv searches CWD when launched via console script#753
fix: load_dotenv searches CWD when launched via console script#753AyushKar2005 wants to merge 1 commit intoTauricResearch:mainfrom
Conversation
load_dotenv() defaults to the script's directory, which breaks .env loading when tradingagents is installed as a package and run from a different directory via the console script entry point. Fix by using find_dotenv(usecwd=True) so dotenv always searches from the user's current working directory upward. Fixes TauricResearch#726
There was a problem hiding this comment.
Code Review
This pull request updates environment variable loading across the project by utilizing find_dotenv(usecwd=True) in cli/main.py and main.py. Additionally, it modifies the default LLM configuration in main.py to use OpenRouter and specific Llama models while removing explanatory comments. The reviewer recommends reverting these configuration changes to maintain the PR's focus on the environment loading fix and to keep the example script accessible to users without specific external API keys.
| config["llm_provider"] = "openrouter" | ||
| config["deep_think_llm"] = "meta-llama/llama-3.3-70b-instruct:free" | ||
| config["quick_think_llm"] = "meta-llama/llama-3.3-70b-instruct:free" | ||
| config["max_debate_rounds"] = 1 | ||
|
|
||
| # Configure data vendors (default uses yfinance, no extra API keys needed) | ||
| config["data_vendors"] = { | ||
| "core_stock_apis": "yfinance", # Options: alpha_vantage, yfinance | ||
| "technical_indicators": "yfinance", # Options: alpha_vantage, yfinance | ||
| "fundamental_data": "yfinance", # Options: alpha_vantage, yfinance | ||
| "news_data": "yfinance", # Options: alpha_vantage, yfinance | ||
| "core_stock_apis": "yfinance", | ||
| "technical_indicators": "yfinance", | ||
| "fundamental_data": "yfinance", | ||
| "news_data": "yfinance", | ||
| } |
There was a problem hiding this comment.
The changes to the LLM provider, model selection, and the removal of descriptive comments appear to be unrelated to the primary goal of fixing .env loading. Switching the default provider to openrouter and using a specific Llama model introduces a dependency on a non-default configuration and external API keys that may not be available to all users. It is recommended to revert these configuration changes to maintain the example script's accessibility and keep the PR focused on the bug fix.
| config["llm_provider"] = "openrouter" | |
| config["deep_think_llm"] = "meta-llama/llama-3.3-70b-instruct:free" | |
| config["quick_think_llm"] = "meta-llama/llama-3.3-70b-instruct:free" | |
| config["max_debate_rounds"] = 1 | |
| # Configure data vendors (default uses yfinance, no extra API keys needed) | |
| config["data_vendors"] = { | |
| "core_stock_apis": "yfinance", # Options: alpha_vantage, yfinance | |
| "technical_indicators": "yfinance", # Options: alpha_vantage, yfinance | |
| "fundamental_data": "yfinance", # Options: alpha_vantage, yfinance | |
| "news_data": "yfinance", # Options: alpha_vantage, yfinance | |
| "core_stock_apis": "yfinance", | |
| "technical_indicators": "yfinance", | |
| "fundamental_data": "yfinance", | |
| "news_data": "yfinance", | |
| } | |
| config["deep_think_llm"] = "gpt-5.4-mini" # Use a different model | |
| config["quick_think_llm"] = "gpt-5.4-mini" # Use a different model | |
| config["max_debate_rounds"] = 1 # Increase debate rounds | |
| # Configure data vendors (default uses yfinance, no extra API keys needed) | |
| config["data_vendors"] = { | |
| "core_stock_apis": "yfinance", # Options: alpha_vantage, yfinance | |
| "technical_indicators": "yfinance", # Options: alpha_vantage, yfinance | |
| "fundamental_data": "yfinance", # Options: alpha_vantage, yfinance | |
| "news_data": "yfinance", # Options: alpha_vantage, yfinance | |
| } |
Problem
Fixes #726
so basically when you install tradingagents as a package and run it
via the console script (just typing
tradingagentsin terminal), the.envfile never gets picked up. all your API keys just dont load andit feels like the .env doesnt exist at all.
Why this happens
turns out
load_dotenv()by default looks for.envrelative towhere the script is, not where you are in the terminal. when you run
python cli/main.pydirectly from the project folder it works finebecause the script is right there. but once its installed as a package
the script lives somewhere deep in site-packages so it never finds
your .env
Fix
changed
load_dotenv()toload_dotenv(find_dotenv(usecwd=True))inboth files. the
usecwd=Truepart makes it search from whateverdirectory the user is actually standing in when they run the command,
which is what you'd expect it to do in the first place honestly.
Files changed
cli/main.py— fixed both load_dotenv calls (the normal one and the .env.enterprise one)main.py— fixed the load_dotenv call there too