0

I have the following files inside a package called users:

file __init__.py:

from flask_sqlalchemy import SQLAlchemy
from .views import UserDetails, UserList
db = SQLAlchemy()

file models.py:

from users import db
class User(db.Model):
 pass

and file views.py:

from .models import User
from users import db
#code

But the following Import exception had occurred:

Error: While importing "users", an ImportError was raised:
Traceback (most recent call last):
 File "/var/www/microservices/venv/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
 __import__(module_name)
 File "/var/www/microservices/Flask_Microservices/users/__init__.py", line 9, in <module>
 from .views import UserDetails, UserList
 File "/var/www/microservices/Flask_Microservices/users/views.py", line 5, in <module>
 from .models import User
 File "/var/www/microservices/Flask_Microservices/users/models.py", line 2, in <module>
 from users import db
ImportError: cannot import name 'db'

Any idea about what is wrong in my imports?

asked Aug 1, 2019 at 9:22
1
  • thank you, no unfortunately, views also use db. @00 Commented Aug 1, 2019 at 9:33

1 Answer 1

4

Wild guess. Try to move

from .views import UserDetails, UserList

under db = SQLAlchemy() so it looks like

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
from .views import UserDetails, UserList

when you execute import from views it tries to import db from init.py in views.py file. It is not present yet so error occurs. At least I think so.

answered Aug 1, 2019 at 9:26
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.