53

Many examples for Flask apps that I have seen have the models stored directly in the main app file (http://pythonhosted.org/Flask-SQLAlchemy/quickstart.html, http://maximebf.com/blog/2012/10/building-websites-in-python-with-flask/). Other ones (http://flask.pocoo.org/docs/patterns/sqlalchemy/) have a "models.py" file in which models are placed.

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

from app import db

class User(db.Model):
    [...]

I get the following error:

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

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

element119
  • 7,475
  • 8
  • 51
  • 74
  • But where is `db` defined? If you create `db` in `database.py` then you need to `from .database import db`, not from `app`. – Martijn Pieters Feb 09 '13 at 15:43
  • `db` is defined in my main `app.py` file: `db = SQLAlchemy(app)`. – element119 Feb 09 '13 at 15:44
  • I don't know where else to define it, because then the "app" variable won't be in scope in any other file. – element119 Feb 09 '13 at 15:45
  • But if `app` is importing from `User` you now have a circular import, and that doesn't work. Move `db` out to a separate module; the Flask patterns documentation you link to has a separate `database.py` module. – Martijn Pieters Feb 09 '13 at 15:46
  • Ok, I have moved the declaration to `database.py`. The problem now though is that I need `app` to instantiate `db`, but the only way I can get it is with `from myapp import app` which is then a circular import. – element119 Feb 09 '13 at 15:49
  • Also, to clarify, I am using `Flask-SQLAlchemy` (http://pythonhosted.org/Flask-SQLAlchemy/), so some of the code on that link is not necessary. – element119 Feb 09 '13 at 15:51
  • But that presumes you use `flask.ext.sqlalchemy`; the other one does not and I don't think you need to have it. – Martijn Pieters Feb 09 '13 at 15:52
  • Yes, but I want to use `flask.ext.sqlalchemy` because it makes configuration much simpler (i.e., http://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#road-to-enlightenment). I just don't know how to access the `app` variable from outside of the main app file. – element119 Feb 09 '13 at 15:57
  • 4
    You do *not* need to import `app`; just create a new instance of `Flask()` with the same name. Use `Flask('yourapplication')` every where, see http://flask.pocoo.org/docs/api/#application-object. That's *one* circular import broken. – Martijn Pieters Feb 09 '13 at 16:07
  • how can I create a folder for the models instead using a single models.py file –  Mar 02 '16 at 02:55

2 Answers2

44

This answer was extremely helpful: https://stackoverflow.com/a/9695045/353878.

I needed to not initialize the db right away.

Community
  • 1
  • 1
element119
  • 7,475
  • 8
  • 51
  • 74
  • 2
    What does that mean, "not initialize the db right away"? – johnny Jan 29 '15 at 22:26
  • 4
    @johnny It means that `SQLAlchemy()` does not have to take `app` as parameter in the module it is used. In most examples you can see `SQLAlchemy(app)` but it requires `app` from other scope in this case. Instead you can use uninitialized `SQLAlchemy()` and use `init_app(app)` method later as described in [http://stackoverflow.com/a/9695045/2040487](http://stackoverflow.com/a/9695045/2040487). – Michał Sep 02 '15 at 17:49
-6
from app.database import Base

class User(Base):
__tablename__ = 'users'

Shouldn it be this way ??

Redian
  • 334
  • 1
  • 7