1

File Structure:

enter image description here

routers/blog.py file

from ..Blog import schema, models

I am in routers/blog.py file. I want to import schema and models files. But I am getting this error.

from ..Blog import schema, models
ImportError: attempted relative import beyond top-level package

I was following this link, but still unable to solve it.

Chris
  • 18,724
  • 6
  • 46
  • 80
Po_
  • 11
  • 2
  • Does this answer your question? [FastAPI: " ImportError: attempted relative import with no known parent package"](https://stackoverflow.com/questions/70874423/fastapi-importerror-attempted-relative-import-with-no-known-parent-package) – Chris Mar 13 '23 at 07:31
  • Please have a look at related answers [here](https://stackoverflow.com/a/71080756/17865804), as well as [here](https://stackoverflow.com/a/73647164/17865804) and [here](https://stackoverflow.com/a/71342222/17865804) – Chris Mar 13 '23 at 07:32

2 Answers2

0

You only need to get rid of ..Blog. The following should work:

import schema, models
0

You can set PYTHONPATH in your environment, then run the FastAPI application in terminal. For example if your root project directory is under your home, like: /home/tashifa/Blog: The export command be like:

export PYTHONPATH=/home/tashifa/Blog:$PYTHONPATH

After that, your imports in blog.py be like:

from routers import schemas, models 
Daniel
  • 61
  • 10