mardi 28 avril 2020

Python - Flask ImportError: cannot import name 'Userauth' from 'models'

I learn to made apps in Flask, I got an error cz I think I made an circular import but I am not sure and really new in Flask. I have this in application.py:

application = Flask(
    __name__, static_folder="client/build", static_url_path="/client/build"
)

DB = SQLAlchemy(application)
jwt = JWTManager(application)

from models import Userauth, Product

...

if __name__ == "__main__":
    from models import Userauth, Product
    application.run()

I know that looks weird because double command from models import Userauth, Product, but if I just write the second command (before application.run()) I got an error to running flask run

my model.py:

from application import DB

class Userauth(DB.Model):
...

class Product(DB.Model):
...

If I run python application.py I got this error:

(venv) /% python application.py
Traceback (most recent call last):
  File "application.py", line 28, in <module>
    from models import Userauth, Product
  File "//models.py", line 1, in <module>
    from application import DB
  File "//application.py", line 28, in <module>
    from models import Userauth, Product
ImportError: cannot import name 'Userauth' from 'models' (/models.py)

Do you know how to deal with this problem? How is the good design pattern in Flask apps should be?

Thank you.

Aucun commentaire:

Enregistrer un commentaire