1
Backend
├── Flaskr
│   └── __init__.py
└── models.py

I want to import model.py into __init__.py

I tried from models import setup_db, Book I tried from backend.models import setup_db, Book

I end up up getting one of these two errors:

from models import setup_db, Book

ModuleNotFoundError: No module named 'models'

or

from backend.models import setup_db, Book

ModuleNotFoundError: No module named 'backend'

Alexander
  • 59,041
  • 12
  • 98
  • 151
Melissa
  • 11
  • 1
  • 1
    Your package is named `Backend`, not `backend`; case matters. Rename the folder from `Backend` to `backend` and try again. And are you sure the folder `Backend` is contained in is in `sys.path`? – ShadowRanger Aug 23 '22 at 21:16
  • This post has the same issue of [this one](https://stackoverflow.com/questions/43728431/relative-imports-modulenotfounderror-no-module-named-x?r=SearchResults&s=1%7C95.2403). Is there something else on yout case that the other hasn't? – Palinuro Aug 23 '22 at 21:22

1 Answers1

-1

Here is something to try, not sure how it will work

First import sys in init.py

import sys

Second, add the path / directory of models.py

sys.path.insert(0, '/the/folder/path/name-package/')

Then, make another file called init.py, leave it blank. Leave it in the same directory as models.py. This is so python knows it is a package.

Lastly, import models.py from the backend folder

from backend import models

I am not sure if this will work but it may be worth a try if other peoples suggestions don't work very well.

Thanks for your time.

Edit: You may also be able to use this instead of using the exact path.

sys.path.insert("../")

this in theory will select the path of the backend by going back a directory.