0

I have the following app structure. IDE works fine, resolves, but running some scripts gives me

 File "/home/sink/TARET/app/models.py", line 4, in <module> from app import db ImportError: No module named app 

error. I call the module as:

import datetime from app import db class Role(db.Model): __tablename__ = 'role' RoleID = db.Column(db.Integer, primary_key=True) Name = db.Column(db.String(80), unique=True) ModifiedDate = db.Column(db.DATETIME) and so on 

What is the correct usage of modules in python? I have the following structure.

enter image description here

Ok edit:

db is defined in init.py as

app = Flask(__name__) app.debug = True app.config.from_object('config') db = SQLAlchemy(app) 
3
  • docs.python.org/2/tutorial/modules.html#the-module-search-path Commented Aug 11, 2014 at 19:13
  • where's db defined? and if app is a module, where is the __init__.py to define it as a module? In a nutshell: your IDE is wrong, python is right: app does not exists, db even less. Commented Aug 11, 2014 at 19:14
  • 1
    where is db coming from? There is no db.py. Commented Aug 11, 2014 at 19:15

1 Answer 1

1

On the bottom of your app/__init__.py put this:

from app import models 

Why?

From http://flask.pocoo.org/docs/patterns/packages/

Circular Imports

Every Python programmer hates them, and yet we just added some: circular imports (That’s when two modules depend on each other. In this case views.py depends on __init__.py). Be advised that this is a bad idea in general but here it is actually fine. The reason for this is that we are not actually using the views in __init__.py and just ensuring the module is imported and we are doing that at the bottom of the file.

Sign up to request clarification or add additional context in comments.

2 Comments

taht actually did it cheers,one more question, i have env.py under alembic folder and i have to call db from init.py file, how can i call it from a parent folder?
You should put the Alembic folder out of the app folder. It's a better design (for me), the migrations is not part of the application at all. Then, you can easyly do a "from app import db"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.