Search code examples
pythonimportfastapisubdirectorysupabase

ModuleNotFoundError: No module named 'src' , trying to import from a sub package


I have this folder structure in my project :

easylaw-api/
│
├── src/
│   ├── api/
│   │   ├── main.py
│   │   ├── __init__.py
│   │   ├── models/
│   │   │   ├── codes.py
│   │   │   └── __init__.py
│   │   │
│   │   ├── routers/
│   │   │   └── __init__.py
│   │   │
│   │   └── services/
│   │       ├── codes_service.py
│   │       └── __init__.py
│   │
│   ├── config/
│   │   ├── doNothing.py
│   │   ├── supabase_client.py
│   │   └── __init__.py

I have this problem Traceback (most recent call last): File "easylaw-api/src/api/main.py", line 3, in from routers import ( File "easylaw-api/src/api/routers/codes.py", line 2, in from services import codes_service File "easylaw-api/src/api/services/codes_service.py", line 7, in from src.config.doNothing import doNothing ModuleNotFoundError: No module named 'src'

The import statement causing the error is:

from src.config.doNothing import doNothing

I understand that this issue might be related to relative imports, but I'm unsure how to resolve it. Any guidance or suggestions would be greatly appreciated. Thank you!

I tried changing the import to this : from ..config.doNothing import doNothing and other variations but nothing worked


Solution

  • You run a python file, and the working folder will be the parent folder of this file. So you're running your project with the working folder as ".\src\api" instead of "." which you may want. The module src is simply not there, so your program cannot find it, no matter with a direct import or a relative import.

    Try moving your main file into the base folder of your project and then switching relative imports accordingly.