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`.
This commit is contained in:
Paul Gauthier 2024-05-02 11:29:12 -07:00
parent 8cfdd36620
commit 254481408e

View file

@ -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