0

My folder structure looks like below

.
└── myfolder/
    └── mock/
        ├── __init__.py
        └── main.py
    └── common/
        ├── __init__.py
        └── db.py
        └── routers
            └── myroute.py
            └── __init__.py
        

My main.py

from fastapi import FastAPI

from ..common.db import create_db_and_tables
from ..common.routers import myroute


app = FastAPI()
app.include_router(myroute.router) 

Now from inside of mock if I run the main.py, I am getting python ImportError: attempted relative import with no known parent package .

I am not sure whats I am missing here

mzjn
  • 48,958
  • 13
  • 128
  • 248
Dibshare
  • 95
  • 2
  • 13
  • 2
    Does this answer your question? [Relative imports in Python 3](https://stackoverflow.com/questions/16981921/relative-imports-in-python-3) – Maurice Meyer Aug 29 '22 at 07:40
  • I did try running with `python -m main.py` inside `mock` . But getting same error `ImportError: attempted relative import with no known parent package` – Dibshare Aug 29 '22 at 07:49
  • See also https://stackoverflow.com/q/14132789/407651 – mzjn Aug 29 '22 at 07:49
  • @Dibshare please have a look at the answers provided (at least at the accepted ones), btw. `python -m main.py` does not make any sense. – Maurice Meyer Aug 29 '22 at 07:54
  • Yes , i got a sense of the issue , import same modules from root is working fine. Just trying to figure out what it takes to run it from inside that `mock` folder – Dibshare Aug 29 '22 at 08:01
  • 1
    I find the posted answers in the comments way too complex and too long! Option a) add the path of myfolder (including myfolder) to the environment variable PYTHONPATH. Option b) add import sys and sys.path.append(r"../../myfolder") as first lines in main.py – svebert Aug 29 '22 at 08:08

0 Answers0