Separate SQLAlchemy models by file in Flask


Many examples for Flask apps that I have seen have the models stored directly in the main app file (, Other ones ( have a "" file in which models are placed.

How can I have my Flask app import models from separate files, e.x. ""? When I try creating a file with these contents:

from app import db

class User(db.Model):

I get the following error:

File "/Users/stackoverflow/myapp/models/", line 1, in <module>
from app import db
ImportError: No module named app

When I insert from models import User in my module file.

2/9/2013 3:37:17 PM

Accepted Answer

This answer was extremely helpful:

I needed to not initialize the db right away.

5/23/2017 11:54:09 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow