Use relative import for Coder class in main.py

# Context:
USER: $ python3 ./aider/main.py
Traceback (most recent call last):
  File "/Users/gauthier/Projects/aider/./aider/main.py", line 5, in <module>
    from aider.coder import Coder
ModuleNotFoundError: No module named 'aider'

ASSISTANT: It seems like there is an issue with importing the `Coder` class from the `aider` package. To fix this, you can try adding the current directory to the `sys.path`. Here's the change you need to make in the `aider/main.py` file:

```python
aider/main.py
<<<<<<< ORIGINAL
import os
import sys
import argparse
from dotenv import load_dotenv
from aider.coder import Coder
=======
import os
import sys
sys.path.append(os.path.dirname(os.path
^C KeyboardInterrupt
USER: i would rather not do that. is there a different way to do the import so that it works both when the aider package is pip installed and also when its being run from the repo checkout?
ASSISTANT: Yes, you can use a relative import instead of an absolute import. This will work both when the package is installed via pip and when it's being run from the repo checkout. Here's the change you need to make in the `aider/main.py` file:

```python
aider/main.py
<<<<<<< ORIGINAL
from dotenv import load_dotenv
from aider.coder import Coder
=======
from dotenv import load_dotenv
from .coder import Coder
>>>>>>> UPDATED
```

This change replaces the absolute import with a relative import, which should resolve the `ModuleNotFoundError`.
This commit is contained in:
Paul Gauthier 2023-05-09 11:34:07 -07:00
parent 8e79a7cf9b
commit a2eb1aff11

View file

@ -2,8 +2,7 @@ import os
import sys
import argparse
from dotenv import load_dotenv
from aider.coder import Coder
from .coder import Coder
def main():
load_dotenv()