From 254481408e5119cc409c357d4fe8849c29fc5009 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 2 May 2024 11:29:12 -0700 Subject: [PATCH] aider: Implemented loading .env file specified by command line argument. # Aider chat conversation: USER: do the todo ASSISTANT: To implement the TODO item in `aider/main.py` about loading a .env file, we can make the following changes: 1. Add the python-dotenv package to the imports. 2. Load the .env file specified by args.env_file using dotenv.load_dotenv(). Here are the *SEARCH/REPLACE* blocks to make those changes: aider/main.py ```python <<<<<<< SEARCH import configparser import os import sys from pathlib import Path import git import litellm from streamlit.web import cli ======= import configparser import os import sys from pathlib import Path import git import litellm from dotenv import load_dotenv from streamlit.web import cli >>>>>>> REPLACE ``` aider/main.py ```python <<<<<<< SEARCH # todo: load .env using filename args.env_file ======= if args.env_file: load_dotenv(args.env_file) >>>>>>> REPLACE ``` This will load the environment variables from the specified .env file, if provided via the `--env-file` command line argument. Note: You'll need to install the `python-dotenv` package for this to work, e.g. via `pip install python-dotenv`. --- aider/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aider/main.py b/aider/main.py index b66104881..9efcc9d58 100644 --- a/aider/main.py +++ b/aider/main.py @@ -5,6 +5,7 @@ from pathlib import Path import git import litellm +from dotenv import load_dotenv from streamlit.web import cli from aider import __version__, models @@ -287,7 +288,8 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F cmd_line = scrub_sensitive_info(args, cmd_line) io.tool_output(cmd_line, log_only=True) - # todo: load .env using filename args.env_file + if args.env_file: + load_dotenv(args.env_file) if args.anthropic_api_key: os.environ["ANTHROPIC_API_KEY"] = args.anthropic_api_key